MySQL vehicle ID problem.
#1

I'll try to explain this problem to best of my ability, basically I have a command '/acar create < model id / model name.'
This inserts a new row in to the MySQL table `VehicleInfo` (a new vehicle). Once this has finished querying, I then send a new query to the database, getting the data from it and then spawning the vehicle. Although, during testing I created 6 vehicles then empty'd the table but when I go in to the server and create another new vehicle it's registering the ID as 7 instead of 1 again (Note: 1 - 6 vehicle ID's nolonger exist within the table)

I also stopped the server then empty'd the table.

Here is some code:

Command:
PHP код:
if(!strcmp(iParams"create"true6)) {
        if(
PlayerInfo[playerid][aAdmin] < 4) return SendError(playeridCANT_USE_CMD);
        if(!
strlen(iParamsEx)) return SCP(playerid"/acar create""< model / id >");
        new 
slctedID;
        if(!
IsNumeric(iParamsEx)) slctedID ModelFromName(iParamsEx);
        else 
slctedID strval(iParamsEx);
        if(
slctedID 400 || slctedID 611) return SCP(playerid"/acar create""< model / id >");
        new 
Float:playerXFloat:playerYFloat:playerZFloat:playerA;
        
GetPlayerPos(playeridplayerXplayerYplayerZ);
        
GetPlayerFacingAngle(playeridplayerA);
        
CreateNewVehicle(PlayerName(playerid), slctedIDplayerXplayerYplayerZplayerA);
        new 
iFormat[159];
        
format(iFormatsizeof(iFormat), "(( AdmCmd | {CCCCCC}You have successfully created a new vehicle: %s! {D13F3F}))"VehicleName[slctedID 400]);
        
SendPlayerMessage(playerid0xD13F3FFFiFormat"(( AdmCmd | {CCCCCC}");
        return 
1;
    } 
CreateNewVehicle function
PHP код:
stock CreateNewVehicle(vehOwner[], vehModelFloat:vehXFloat:vehYFloat:vehZFloat:vehAvVW 0rRes VEHICLE_OWNED) {
    new 
iQuery[450];
    
mysql_format(PipelineiQuerysizeof(iQuery), "INSERT INTO `VehicleInfo` (`Owner`, `Model`, `X`, `Y`, `Z`, `A`, `VirtualWorld`, `Type`) VALUES ('%e', %d, %f, %f, %f, %f, %d, %d)"vehOwnervehModelvehXvehYvehZvehAvVWrRes);
    
mysql_tquery(PipelineiQuery"OnVehicleCreation");

OnVehicleCreation
PHP код:
public OnVehicleCreation() {
    new 
vehicleid cache_insert_id();
    
printf("[ON VEHICLE CREATE] %d has been created successfully!"vehicleid);
    new 
iQuery[159]; mysql_format(PipelineiQuerysizeof(iQuery), "SELECT * FROM `VehicleInfo` WHERE `ID` = %d LIMIT 1"vehicleid);
    
mysql_tquery(PipelineiQuery"OnLoadVehicles""d"0);
    return 
1;

Reply
#2

That's normal. Auto-generated ID's do not get reset or reused. And with reason. For example, this very topic has id 634613 in the database. If it gets deleted then that ID does not get re-used. If it did get reused then all existing links to it would suddenly point to another topic. If you want to start from scratch you should use TRUNCATE TABLE. This deletes all the data and it also resets the auto-increment id. However I don't believe truncate will work if there are foreign keys.

Quote:
Originally Posted by Bwandon
Посмотреть сообщение
Once this has finished querying, I then send a new query to the database, getting the data from it and then spawning the vehicle.
Why? That's extremely counter intuitive. You already have the data so why (again) do you need to query for it?

Also what's the deal with prefixing variables with "i"? When I see "i" in front of a variable I think "integer". This makes your code a bitch to read because none of those variables are integers.
Reply
#3

I understand now.

Sorry, it's a shitty habit. I'll try to get out of it.

And I'll fix the code when inserting a new row, like you said it's a waste of time to send a new query.

Thanks for your reply.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
That's normal. Auto-generated ID's do not get reset or reused. And with reason. For example, this very topic has id 634613 in the database. If it gets deleted then that ID does not get re-used. If it did get reused then all existing links to it would suddenly point to another topic. If you want to start from scratch you should use TRUNCATE TABLE. This deletes all the data and it also resets the auto-increment id. However I don't believe truncate will work if there are foreign keys.



Why? That's extremely counter intuitive. You already have the data so why (again) do you need to query for it?

Also what's the deal with prefixing variables with "i"? When I see "i" in front of a variable I think "integer". This makes your code a bitch to read because none of those variables are integers.
Just a small point, you can reset the ID's via
pawn Код:
ALTER TABLE DROP `somecolumname`
ALTER TABLE `sometable` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (`id`);
I don't recommend doing this. I was merely posting on how to reset it. You should never ever need, or use this method if your mode relies on ID's (stats, achievements etc). It can cause issues where the right data isn't being selected for a certain ID
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)