Delete MySQL
#1

How to do that after entering the VID in the dialogue, the vehicle is removed and from the database.
Код:
DeleteVehicle(vehicleID)
{
	new query[100], id;

	VehicleInfo[id][vID] = DestroyVehicle(vehicleID);

	mysql_format(g_SQL, query, sizeof query, "DELETE FROM `vehicles` WHERE `vehicle_id` = %d LIMIT 1", Veh[VehicleInfo[id][vID]]);
	mysql_tquery(g_SQL, query);
	return 1;
}

case 999:
{
	if(response)
	{
		if(!strlen(inputtext)) return ShowPlayerDialog(playerid, 999, DIALOG_STYLE_INPUT, "Delete Vehicle", "", "Delete", "Back");
		if(!isNumeric(inputtext)) return ShowPlayerDialog(playerid, 999, DIALOG_STYLE_INPUT, "Delete Vehicle", "", "Delete", "Back");
		if(strval(inputtext) < 0) return ShowPlayerDialog(playerid, 999, DIALOG_STYLE_INPUT, "Delete  Vehicle", "", "Delete", "Back");

		DeleteVehicle(strval(inputtext));
	}
	else
	{ 
		callcmd::panelvehicles(playerid, "");
	}
}
Reply
#2

In the database I have VID 1 = Sultan, 3 = Elegy and 4 = NRG. And on the server 1 = Sultan, 2 = Elegy and 3 = NRG. If he wants to remove the vehicle with VID 2 = Elegy, he removes from the server and the database is not deleted. Why?
Reply
#3

In case you don't know using
pawn Код:
sizeof query
is absolutely fine. "sizeof" is just a keyword and not a function. You would not do
pawn Код:
new a = (b);
or would you?

Anyway why would you use the return value of "DestroyVehicle" inside your query. It returns 1 on success and 0 on failure.
Is there any good reason why you would store vehicle IDs into a database in the first place?
Reply
#4

I do not remove the vehicle or after the VID the bad vehicle is removed from the database. How to repair?

Код:
forward LoadVehicle();
public LoadVehicle()
{
	new rows = cache_num_rows();
	new id, loaded;
	new str[150];
 	if(rows)
  	{
		while(loaded < rows)
		{
  			cache_get_value_name_int(loaded, "vehicle_id", id);

			VehicleInfo[id][vID] = CreateVehicle(522, 0.0, 0.0, 0.0, 0.0, -1, -1, -1);
			Veh[VehicleInfo[id][vID]] = id;
			SetVehicleToRespawn(VehicleInfo[id][vID]);

			loaded++;

			format(str, sizeof(str), "(UID: %d)", VehicleInfo[id][vID]);
			VehicleInfo[id][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
			Attach3DTextLabelToVehicle(VehicleInfo[id][vLabel], VehicleInfo[id][vID], 0.0, 0.0, 1.0);
	    }
	}
	printf("[Vehicle] %d loaded vehicles.", loaded);
	return 1;
}

CreateVehicle_CV(model, price, Float:posx, Float:posy, Float:posz, Float:posa, color1, color2)
{
	new id, query[300], str[150];

	VehicleInfo[id][vID] = CreateVehicle(522, 0.0, 0.0, 0.0, 0.0, -1, -1, -1);

	Veh[VehicleInfo[id][vID]] = id;

	SetVehicleToRespawn(VehicleInfo[id][vID]);

	format(query, sizeof(query), "INSERT INTO `vehicles` (`vehicle_id`) VALUES ('%d')", id);
	mysql_tquery(g_SQL, query);

	format(str, sizeof(str), "(UID: %d)", VehicleInfo[id][vID]);
	VehicleInfo[id][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
	Attach3DTextLabelToVehicle(VehicleInfo[id][vLabel], VehicleInfo[id][vID], 0.0, 0.0, 1.0);

	return VehicleInfo[id][vID];
}

DeleteVehicle(vehicleID)
{
	new query[100];

	DestroyVehicle(vehicleID);

	mysql_format(g_SQL, query, sizeof(query), "DELETE FROM `vehicles` WHERE `vehicle_id` = %d LIMIT 1", VehicleInfo[vehicleID][vID]);
	mysql_tquery(g_SQL, query);
	return 1;
}

SaveVehicle(xvehid)
{
	new query[612];

	mysql_format(g_SQL, query, sizeof query, "UPDATE `vehicles` SET `vehicle_name` = '%s' WHERE `vehicle_id` = %d LIMIT 1", GetVehicleNameFromModel(VehicleInfo[xvehid][vModel]), xvehid);
	mysql_tquery(g_SQL, query);
	return 1;
}

CMD:dell(playerid, params[])
{
	new v_id;
	if(sscanf(params, "d", v_id)) return SendClientMessage(playerid, -1, "");

	DeleteVehicle(v_id);
	return 1;
}
Reply
#5

I have a problem with vID.
Reply
#6

Or I need to save the vehicles in OnGameModeExit. How?
Reply
#7

Will someone fix? I have a problem with vID. Please help. The whole code is written from scratch here.
It is a vehicle creation system.
Код:
#include <a_samp>
#include <a_mysql>
#include <Pawn.CMD>
#include <sscanf2>

main(){}

#define		MYSQL_HOST 			"127.0.0.1"
#define		MYSQL_USER 			"root"
#define		MYSQL_PASSWORD 		"12345"
#define		MYSQL_DATABASE 		"db"

new MySQL: g_SQL;

enum vInfo
{
	vID,
	vModel,
	Text3D:vLabel
};
new VehicleInfo[MAX_VEHICLES][vInfo];

public OnGameModeInit()
{
	Connect();
	SetupVehicleTable();
	mysql_tquery(g_SQL, "SELECT * FROM `veh`", "LoadVehicles");
	return 1;
}

public OnGameModeExit()
{
	new saved;
	if(cache_num_rows())
	{
		while(saved < cache_num_rows())
		{
			SaveVehicle(saved);

			saved++;
		}
	}
	printf("%d saved vehicles", saved);

	mysql_close(g_SQL);
	return 1;
}

Connect()
{
	new MySQLOpt: option_id = mysql_init_options();
	
	mysql_set_option(option_id, AUTO_RECONNECT, true);

	g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
	if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
	{
		print("OFF SERVER!");
		SendRconCommand("exit");
		return 1;
	}
	else
	{
		print("ON SERVER!");
	}
	return 1;
}

SetupVehicleTable()
{
	new query[1024];
	
	strcat(query, "CREATE TABLE IF NOT EXISTS `veh` (`vehid` int(11), `vehmodel` int(5) default '0', PRIMARY KEY (`vehid`), UNIQUE KEY `vehid_2` (`vehid`), KEY `vehid` (`vehid`))");
	mysql_tquery(g_SQL, query);
	return 1;
}

CreateVehicle_2(model)
{
	new ID, query[512], str[512];

	VehicleInfo[ID][vID] = CreateVehicle(model, -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);
	VehicleInfo[ID][vModel] = model;

	SetVehicleToRespawn(VehicleInfo[ID][vID]);

	mysql_format(g_SQL, query, sizeof(query), "INSERT INTO `veh` (`vehid`, `vehmodel`) VALUES ('%d', '%d')", VehicleInfo[ID][vID], model);
	mysql_tquery(g_SQL, query);

	format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", VehicleInfo[ID][vID]);
	VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
	Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], VehicleInfo[ID][vID], 0.0, 0.0, 1.0);
	return 1;
}

forward LoadVehicles();
public LoadVehicles()
{
	new loaded, ID, str[512];
	if(cache_num_rows())
	{
		while(loaded < cache_num_rows())
		{
			cache_get_value_int(loaded, "vehid", VehicleInfo[ID][vID]);
			cache_get_value_int(loaded, "vehmodel", VehicleInfo[ID][vModel]);

			VehicleInfo[ID][vID] = CreateVehicle(VehicleInfo[ID][vModel], -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);

			SetVehicleToRespawn(VehicleInfo[ID][vID]);

			loaded++;

			format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", VehicleInfo[ID][vID]);
			VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
			Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], VehicleInfo[ID][vID], 0.0, 0.0, 1.0);
		}
	}
	printf("%d loaded vehicles", loaded);
	return 1;
}

SaveVehicle(S_VID)
{
	new query[512];
	mysql_format(g_SQL, query, sizeof(query), "UPDATE `veh` SET `vehid` = %d, `vehmodel` = %d WHERE `vehid` = %d LIMIT 1", VehicleInfo[S_VID][vID], VehicleInfo[S_VID][vModel], VehicleInfo[S_VID][vID]);
	mysql_tquery(g_SQL, query);
	return 1;
}

DeleteVehicle(D_VID)
{
	new query[512], ID, Veh[MAX_VEHICLES];

	VehicleInfo[ID][vID] = DestroyVehicle(D_VID);
	Veh[VehicleInfo[ID][vID]] = D_VID;

	mysql_format(g_SQL, query, sizeof(query), "DELETE FROM `veh` WHERE `vehid` = %d LIMIT 1", D_VID);
	mysql_tquery(g_SQL, query);
	return 1;
}

CMD:create(playerid, params[])
{
	new model;
	if(sscanf(params, "d", model)) return SendClientMessage(playerid, -1, "USAGE: /create [MODEL ID]");
	CreateVehicle_2(model);
	return 1;
}

CMD:delete(playerid, params[])
{
	new del_vid;
	if(sscanf(params, "d", del_vid)) return SendClientMessage(playerid, -1, "USAGE: /delete [VID]");
	DeleteVehicle(del_vid);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerSkin(playerid, 29);
	SetPlayerPos(playerid, -23.9678, 30.5966, 3.1172);
	SetPlayerFacingAngle(playerid, 324.9064);
	return 1;
}
Reply
#8

If you really want to store vehicles, you should forget about storing vehicle IDs. In SA:MP IDs are used to describe objects exist in that instance. If that instance no longer exists (shutting down server), IDs are no longer valid. For vehicles you need to store a key, vehicle model, vehicle position and rotation, tuning parts and etc.. At creation you can set up a collection (mostly an array does a fine job) that contains all spawned vehicle IDs associated to their keys inside the database.
Reply
#9

Some systems have vehicle IDs. How to make this key?
Reply
#10

Код:
#include <a_samp>
#include <a_mysql>
#include <zcmd>
#include <sscanf2>

main(){}

#define		MYSQL_HOST 			"127.0.0.1"
#define		MYSQL_USER 			"root"
#define		MYSQL_PASSWORD 		""
#define		MYSQL_DATABASE 		"db"

new MySQL: g_SQL;

enum vInfo
{
	SQLID,
	vModel,
	Text3D:vLabel
};
new VehicleInfo[MAX_VEHICLES][vInfo];

public OnGameModeInit()
{
	AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
	Connect();
	SetupVehicleTable();
	mysql_tquery(g_SQL, "SELECT * FROM `veh`", "LoadVehicles");
	return 1;
}

public OnGameModeExit()
{
	new saved;
	if(cache_num_rows())
	{
		while(saved < cache_num_rows())
		{
			SaveVehicle(saved);

			saved++;
		}
	}
	printf("%d saved vehicles", saved);

	mysql_close(g_SQL);
	return 1;
}

Connect()
{
	new MySQLOpt: option_id = mysql_init_options();

	mysql_set_option(option_id, AUTO_RECONNECT, true);

	g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
	if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
	{
		print("OFF SERVER!");
		SendRconCommand("exit");
		return 1;
	}
	else
	{
		print("ON SERVER!");
	}
	return 1;
}

SetupVehicleTable()
{
	mysql_tquery(g_SQL, "CREATE TABLE IF NOT EXISTS `veh` (`vehid` int(11), `vehmodel` int(5) default '0', PRIMARY KEY (`vehid`), UNIQUE KEY `vehid_2` (`vehid`), KEY `vehid` (`vehid`))");
	mysql_tquery(g_SQL, "ALTER TABLE `veh` CHANGE `vehid` `vehid` INT(11) NOT NULL AUTO_INCREMENT;");
	return 1;
}

CreateVehicle_2(model)
{
	new query[512], str[512];

	new ID = CreateVehicle(model, -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);
	VehicleInfo[ID][vModel] = model;
	
	SetVehicleToRespawn(ID);

	mysql_format(g_SQL, query, sizeof(query), "INSERT INTO `veh` (`vehmodel`) VALUES ('%d')",model);
	mysql_tquery(g_SQL, query,"OnVehicleCreate","i",ID);

	format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", ID);
	VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
	Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], ID, 0.0, 0.0, 1.0);
	return 1;
}

forward OnVehicleCreate(vehicleid);
public OnVehicleCreate(vehicleid)
{
	VehicleInfo[vehicleid][SQLID] = cache_insert_id();
	return 1;
}


forward LoadVehicles();
public LoadVehicles()
{
	new loaded, ID, str[512];
	if(cache_num_rows())
	{
		while(loaded < cache_num_rows())
		{
			ID = CreateVehicle(VehicleInfo[ID][vModel], -23.9678, 30.5966, 3.1172, 324.9064, -1, -1, -1);
            cache_get_value_int(loaded, "vehid", VehicleInfo[ID][SQLID]);
			cache_get_value_int(loaded, "vehmodel", VehicleInfo[ID][vModel]);

			SetVehicleToRespawn(ID);

			loaded++;

			format(str, sizeof(str), "{0b62ed}VID: {00d820}%d", ID);
			VehicleInfo[ID][vLabel] = Create3DTextLabel(str, -1, 0.0, 0.0, 0.0, 50.0, 0);
			Attach3DTextLabelToVehicle(VehicleInfo[ID][vLabel], ID, 0.0, 0.0, 1.0);
		}
	}
	printf("%d loaded vehicles", loaded);
	return 1;
}

SaveVehicle(S_VID)
{
	new query[512];
	mysql_format(g_SQL, query, sizeof(query), "UPDATE `veh` SET `vehmodel` = %d WHERE `vehid` = %d LIMIT 1", VehicleInfo[S_VID][vModel], VehicleInfo[S_VID][SQLID]);
	mysql_tquery(g_SQL, query);
	return 1;
}

DeleteVehicle(D_VID)
{
	new query[512];

	DestroyVehicle(D_VID);
	
	mysql_format(g_SQL, query, sizeof(query), "DELETE FROM `veh` WHERE `vehid` = %d LIMIT 1", VehicleInfo[D_VID][SQLID]);
	mysql_tquery(g_SQL, query);
	
	VehicleInfo[D_VID][SQLID] = 0;
	VehicleInfo[D_VID][vModel] = 0;
	return 1;
}

CMD:create(playerid, params[])
{
	new model;
	if(sscanf(params, "d", model)) return SendClientMessage(playerid, -1, "USAGE: /create [MODEL ID]");
	CreateVehicle_2(model);
	return 1;
}

CMD:delete(playerid, params[])
{
	new del_vid;
	if(sscanf(params, "d", del_vid)) return SendClientMessage(playerid, -1, "USAGE: /delete [VID]");
	DeleteVehicle(del_vid);
	return 1;
}

public OnPlayerSpawn(playerid)
{
	SetPlayerSkin(playerid, 29);
	SetPlayerPos(playerid, -23.9678, 30.5966, 3.1172);
	SetPlayerFacingAngle(playerid, 324.9064);
	return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)