Separate VehicleID In Database

Started by Saiyan Attack, Nov 06, 2016, 07:06 PM

Previous topic - Next topic

Saiyan Attack

Hello Everyone Who Read This Topic !!

I Want Know That How To Reset Vehicle_ID's In Database For Example I Have 182 Vehicles In My Sqlite Database And I Remove VehicleID 180 From Database When Server Is Online But When I Restart Them So Left I Have 181 Vehicles In My Database I Want To Reset Their ID's From Database .... I Use This Code But It Doesn't Work :( ....

Here Is The Code:-
function ResetIDs()
{
local i=1;
while(i <= GetVehicleCount()) {
QuerySQL(db,"UPDATE Vehicles SET Vehicle_ID='"+i+"' WHERE rowid='"+i+"'");
i ++;
}
print("Total Loaded Separated VehicleID's - "+i);
}

Screen:

.

#1
You need to reset the auto-increment for that particular table (I.e. the sequence table). Which can be done in several ways.

QuerySQL(db, "UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='your_table'");
QuerySQL(db, "DELETE FROM [sqlite_sequence] WHERE [name]='your_table'");
NOTE: If you're using the official plugin which uses a 2+ year old SQLite library. I'm not sure which of them will work.

WARNING: Please be careful with this and do not alter it while there are rows in the table. If I'm not mistaken, primary keys have a UNIQUE constraint. And depending on how SQLite will react. It could either fail to insert new rows because there's already one with that ID. Or, overwrite the values in the existing ones if you're using something like `REPLACE INTO` or `INSERT OR REPLACE INTO`.
.

Anik

No need to do such things. You can do it easily from the sqlite browser by executing a code

UPDATE Vehicles SET Vehicle_ID=rowid

Saiyan Attack

@. I'm Using Plugins Version 04rel004 Patch v7.7 Which (I Downloaded It From Here) ... And The Query Doesn't Work I Use Them Something Like That:-
QuerySQL(db, "UPDATE SQLITE_SEQUENCE SET SEQ=0 WHERE NAME='Vehicles'");
@Anik I Also Tried To Do With Your Way But It Doesn't Work :'(

Screen:


Anik

Did you executed the query by clicking "Execute query"??

Thijn

Quote from: Anik on Nov 08, 2016, 06:16 AMDid you executed the query by clicking "Execute query"??
He did, otherwise it wouldn't say No errors.

The query did it's thing, what does your table look like now?

Anik

Quote from: Thijn on Nov 08, 2016, 07:13 AM
Quote from: Anik on Nov 08, 2016, 06:16 AMDid you executed the query by clicking "Execute query"??
He did, otherwise it wouldn't say No errors.

The query did it's thing, what does your table look like now?

Yeah but it should work. I have did this many time before too

Saiyan Attack

#7
@Thijn Table Look Like As You Can See In First Pic Nothing Will Be Change ....
@Anik I Request You To Use This For Reset IDs ....
Problem Solved By This Code Which Are Think By Me :D
function ResetIDs()
{
local q = QuerySQL(db,"SELECT Vehicle_ID FROM Vehicles");
for(local i = 1; i < 2500; i++)
{
local veh = FindVehicle( i );
if (veh) {
QuerySQL(db"UPDATE Vehicles SET Vehicle_ID='"+veh.ID+"' WHERE Vehicle_ID='"+GetSQLColumnData(q,0)+"'");
GetSQLNextRow( q );
}
}
}
Thanks To All Of Your Reply !! Salute Sirs :) ...