Mysql Vehicle system problem
#1

Hello ladies and gentlemens.

I'm currently working on learning SA:MP mysql scripting. I'm using the mysql plugin made by BlueG, R7.

So far, i have managed to make a fully working account system so i'm working on making the vehicle system. I'm done with the vehicle loading, and the command i'm using to add vehicles into the database.

My problem here is, i'm not able to make a fully working vehicle update system. I have been trying to get it working for around a week now without success. At this point, the data isn't updating at all. Previous problem i had is all the vehicle data is resetting to 0 in the database, vehicle data is over writing itself and all other kind of stuff..

Below is my current code:

Код:
stock CustomVehicleID(vehid)
{
	for(new i; i < sizeof(CarInfo); i++)
	{
		if(CarInfo[i][cVehID] == vehid)
		{
    		return CarInfo[i][cID];
		}
	}
	return -1;
}
Код:
forward LoadVehicles();
public LoadVehicles()
{
	format(dquery, sizeof(dquery), "SELECT * FROM vehicles");
	mysql_function_query(1, dquery, true, "LoadVehicleStats", "i", sizeof(CarInfo));
	return 1;
}

forward LoadVehicleStats(limit);
public LoadVehicleStats(limit)
{
	new rows, fields, count, data[24];
	cache_get_data(rows, fields);
	for(new i; i < rows; i++)
	{
		if(count > limit)
		{
			print("Number of vehicles in database exceeds limit!");
			return 1;
		}
		for(new j; j < sizeof(CarInfo); j++)
		{
			if(CarInfo[j][cID] == 0){
				cache_get_field_content(i, "ID", data); CarInfo[j][cID] = strval(data);
				cache_get_field_content(i, "VehicleID", data); CarInfo[j][cVehID] = strval(data);
				cache_get_field_content(i, "SpawnX", data); CarInfo[j][cSpawnx] = floatstr(data);
				cache_get_field_content(i, "SpawnY", data); CarInfo[j][cSpawny] = floatstr(data);
				cache_get_field_content(i, "SpawnZ", data); CarInfo[j][cSpawnz] = floatstr(data);
				cache_get_field_content(i, "Angle", data); CarInfo[j][cAngle] = floatstr(data);
				cache_get_field_content(i, "Model", data); CarInfo[j][cModel] = strval(data);
				cache_get_field_content(i, "Color1", data); CarInfo[j][cColor1] = strval(data);
				cache_get_field_content(i, "Color2", data); CarInfo[j][cColor2] = strval(data);
				CarInfo[j][cVehID] = AddStaticVehicleEx(CarInfo[j][cModel],CarInfo[j][cSpawnx],CarInfo[j][cSpawny],CarInfo[j][cSpawnz],CarInfo[j][cAngle],CarInfo[j][cColor1],CarInfo[j][cColor2],3600);
				count++;
				break;
			}
		}
	}
	return 1;
}

COMMAND:setvehicle(playerid, params[])
{
	new id,c1,c2,vehicleid,model;
	new name[MAX_PLAYER_NAME], playerstate = GetPlayerState(playerid); if(playerstate != PLAYER_STATE_DRIVER) return SendClientMessage(playerid, COLOR_LIGHTRED, "You need to be in a vehicle to be able to use this command.");
	if(sscanf(params, "iii", id, c1,c2)) return SendClientMessage(playerid, COLOR_LIGHTRED, "USAGE: /setvehicle  [id] [color 1] [color 2]");

	new Float:X, Float:Y, Float:Z, Float:Angle;
	vehicleid = GetPlayerVehicleID(playerid);
	GetVehiclePos(vehicleid,X,Y,Z);
	GetVehicleZAngle(vehicleid,Angle);
	model = GetVehicleModel(vehicleid);
	GetPlayerName(playerid, name, sizeof(name));
	CarInfo[id][cID] = id;
	CarInfo[id][cVehID] = vehicleid;
	CarInfo[id][cSpawnx] = X;
	CarInfo[id][cSpawny] = Y;
	CarInfo[id][cSpawnz] = Z;
	CarInfo[id][cAngle] = Angle;
	CarInfo[id][cModel] = model;
	CarInfo[id][cColor1] = c1;
	CarInfo[id][cColor2] = c2;
	DestroyVehicle(CarInfo[id][cVehID]);
   	format(dquery, sizeof(dquery), "SELECT * FROM vehicles WHERE ID = %d", id);
   	mysql_function_query(1, dquery, false, "SQLAddVehicleToDatabase", "ii", playerid,id);
	AddStaticVehicleEx(model,X,Y,Z,Angle,c1,c2,3600);
	PutPlayerInVehicle(playerid,CarInfo[id][cVehID],0);
	return 1;
}

forward SQLAddVehicleToDatabase(playerid,id);
public SQLAddVehicleToDatabase(playerid,id)
{
	mysql_store_result();
	if(mysql_num_rows() == 0){
		format(dquery, sizeof(dquery), "INSERT INTO vehicles (ID, VehicleID,  Model, SpawnX, SpawnY, SpawnZ, Angle, Color1, Color2) VALUES (%d, %d, %d, %f, %f, %f, %f, %d, %d)",
		CarInfo[id][cID],
		CarInfo[id][cVehID],
		CarInfo[id][cModel],
		CarInfo[id][cSpawnx],
		CarInfo[id][cSpawny],
		CarInfo[id][cSpawnz],
		CarInfo[id][cAngle],
		CarInfo[id][cColor1],
		CarInfo[id][cColor2]);
		mysql_query(dquery,0,0,1);
		SendClientMessage(playerid, COLOR_LIGHTBLUE, "Vehicle has been set.");
	}
	else SendClientMessage(playerid, COLOR_LIGHTRED, "ERROR: That Vehicle ID already exist in the database.");
	mysql_free_result();
	return 1;
}

public OnGameModeExit()
{
	for(new i = 0; i < sizeof(CarInfo); i++) { UpdateVehicle(i);}
	mysql_close();
	return 1;
}
The code specificly i have problem with is below:

Код:
forward UpdateVehicle(id);
public UpdateVehicle(id)
{
	if(CarInfo[id][cID] != 0){
		format(dquery, sizeof(dquery), "UPDATE vehicles SET ID = %d, VehicleID = %d, Model = %d, SpawnX = %f, SpawnY = %f, SpawnZ = %f, Angle = %f, Color1 = %d, Color2 = %d WHERE ID = %d",
		CarInfo[id][cID],
		CarInfo[id][cVehID],
		CarInfo[id][cModel],
		CarInfo[id][cSpawnx],
		CarInfo[id][cSpawny],
		CarInfo[id][cSpawnz],
		CarInfo[id][cAngle],
		CarInfo[id][cColor1],
		CarInfo[id][cColor2],
		id);
		mysql_query(dquery,0,0,1);
	} 
	return 1;
}
Simply, i have run out of ideas and i have no idea on how to properly do it. Anyone here that is familiar with R7 that could give me some tips?

Thanks!
Reply
#2

If I'm not mistaken, you can't send another query if the result of the previous query has not been freed yet. I can be wrong, but I believe you'll get a Command Out Of Sync error. Enable the debug to be sure.
Reply
#3

Actually, i pasted the wrong code which i had problem with. That was from when i was experimenting yesterday to see if it worked.

Updated it now to the current code.
Reply
#4

Up we go. Still got problem with this.
Reply
#5

can you add the code below somewhere in to your GM and paste here the console output?

pawn Код:
public OnQueryError(errorid, error[], resultid, extraid, callback[], query[], connectionHandle)
{
    printf("%d %s %d %d %s %s %d", errorid, error, resultid, extraid, callback, query, connectionHandle);
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)