13.02.2019, 09:26
I'm scripting a vehicle system and made this /createvehicle command, which insert created vehicle in the "vehicles" table:
But it doesn't work. VehicleDBID is set as auto increment but everytime i create a vehicle, its DBID is -1. Also i'm getting "duplicate entry key for DBID -1" in error.log.
And in "vehicles" table all the vehicle data (model, price, color etc) is blank, but it shows the correct executing query in mysql log.
Now i know the problem is in that sql INSERT query, how to fix it?
Error log:
Also i don't know why i'm getting no active cache since i'm connected to the database.
pawn Код:
CMD:createvehicle(playerid, params[])
{
if(Vehicle_Nearest(playerid) != -1) return SCM(playerid, COLOR_ERROR, "* You can't create a vehicle near another one.");
new modelid[30], vehid, color1, color2, price;
if(sscanf(params, "s[30]iid", modelid, color1, color2, price)) return SCM(playerid, COLOR_BELGREEN, "* [USAGE]: /createvehicle <ModelID/Vehicle Name> <Color1> <Color2> <Price>");
if(IsNumeric(modelid)) vehid = strval(modelid);
else vehid = ReturnVehicleModelID(modelid);
if(vehid < 400 || vehid > 611) return SCM(playerid, COLOR_ERROR, "* Invalid vehicle model.");
if(IsPlayerInAnyVehicle(playerid)) return SCM(playerid, COLOR_ERROR, "* You must be on foot to create a vehicle.");
static Float:x, Float:y, Float:z, Float:angle;
GetPlayerPos(playerid, x, y, z);
GetPlayerFacingAngle(playerid, angle);
for (new i = 0; i != MAX_VEH; i ++) if (!VehicleData[i][vehicleExists])
{
VehicleData[i][vehicleExists] = true;
VehicleData[i][vehicleDBID] = cache_insert_id();
SetVehicleNumberPlate(VehicleData[i][vehiclePlate], "SSRP");
VehicleData[i][vehicleModel] = vehid;
format(VehicleData[i][vehicleName], MAX_PLAYER_NAME, GetVehicleName(vehid));
format(VehicleData[i][vehicleOwner], MAX_PLAYER_NAME, "-");
format(VehicleData[i][vehiclePlate], 16, "SSRP");
VehicleData[i][vehiclePrice] = price;
VehicleData[i][vehicleLock] = 0;
VehicleData[i][vehicleColorOne] = color1;
VehicleData[i][vehicleColorTwo] = color2;
VehicleData[i][vehiclePos][0] = x;
VehicleData[i][vehiclePos][1] = y;
VehicleData[i][vehiclePos][2] = z;
VehicleData[i][vehiclePos][3] = angle;
VehicleData[i][vehicleInterior] = GetPlayerInterior(playerid);
VehicleData[i][vehicleWorld] = GetPlayerVirtualWorld(playerid);
VehicleData[i][vehicleOwned] = 0;
new query[1000];
format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleName = '%e', vehicleOwner = '%e', vehiclePlate = '%e'",
VehicleData[i][vehicleDBID], VehicleData[i][vehicleName], VehicleData[i][vehicleOwner], VehicleData[i][vehiclePlate]);
mysql_query(mysql, query);
format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleModel = %d, vehiclePrice = %d, vehicleColorOne = %d, vehicleColorTwo = %d",
VehicleData[i][vehicleDBID], VehicleData[i][vehicleModel], VehicleData[i][vehiclePrice], VehicleData[i][vehicleColorOne], VehicleData[i][vehicleColorTwo]);
mysql_query(mysql, query);
format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleX = %f, vehicleY = %f, vehicleZ = %f, vehicleA = %f",
VehicleData[i][vehicleDBID], VehicleData[i][vehiclePos][0], VehicleData[i][vehiclePos][1], VehicleData[i][vehiclePos][2], VehicleData[i][vehiclePos][3]);
mysql_query(mysql, query);
format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleInterior = %d, vehicleWorld = %d, vehicleOwned = %d",
VehicleData[i][vehicleDBID], VehicleData[i][vehicleInterior], VehicleData[i][vehicleWorld], VehicleData[i][vehicleOwned]);
mysql_query(mysql, query);
Vehicle_Save(i);
Vehicle_Refresh(i);
if(!VehicleData[i][vehicleExists]) return 0;
}
return 1;
}
And in "vehicles" table all the vehicle data (model, price, color etc) is blank, but it shows the correct executing query in mysql log.
Now i know the problem is in that sql INSERT query, how to fix it?
Error log:
Quote:
[11:30:34] [plugins/mysql] cache_insert_id: no active cache (C:\test\v.pwn:376) [11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleName = 'e', vehicleOwner = 'e', vehiclePlate = 'e'": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:407) [11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleModel = 411, vehiclePrice = 5000, vehicleColorOne = 200, vehicleColorTwo = 50": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:411) [11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleX = 1299.185913, vehicleY = -801.424926, vehicleZ = 84.140625, vehicleA = 269.527404": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:415) [11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleInterior = 0, vehicleWorld = 0, vehicleOwned = 0": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:419) [11:30:34] [plugins/mysql] cache_insert_id: no active cache (C:\test\v.pwn:376) |