MySQL -- Free ID Slot -
yoran765 - 10.06.2014
Ello!
Been trying to figure out how MySQL works, kinda got the hang of it untill i stumbled across a problemo!
PHP код:
Vehicle ID 1 - Owner BLBLA
Vehicle ID 4 - Owner BLBLA
Vehicle ID 5 - Owner BLBLA
Vehicle ID 7 - Owner BLBLA
Is there any way to find a free Vehicle ID slot? In the example above : ID 2, ID 3, and ID 6. These would be created in order. I seriously have no idea what I have to do.
Yoran.
Re: MySQL -- Free ID Slot -
Sledgehammer - 10.06.2014
If you want to insert a unique ID into the database use this code:
I hope this is what you mean.
EDIT: You could find an example on how this is used on SA-MP wiki (
https://sampwiki.blast.hk/wiki/MySQL)
Re: MySQL -- Free ID Slot -
iZN - 10.06.2014
Search about AUTO_INCREMENT and use mysql_insert_id or cache_insert_id (if you're using the latest release of BlueG's plugin) and just SELECT your data from the table and put the vehicle IDs and names in a/an variable/array.
Re: MySQL -- Free ID Slot -
yoran765 - 10.06.2014
PHP код:
[21:08:52] CMySQLHandler::Query(INSERT INTO Vehicles (VehicleID, VehicleOwner) VALUES ('1', 'Bitch')) - An error has occured. (Error ID: 1062, Duplicate entry '1' for key 'PRIMARY')
[21:08:52] >> mysql_insert_id( Connection handle: 1 )
PHP код:
COMMAND:test(playerid, params[])
{
new query[500];
format(query, sizeof(query), "INSERT INTO Vehicles (VehicleID, VehicleOwner) VALUES ('%d', 'Bitch')", mysql_insert_id());
mysql_query(query);
printf("ID %d", mysql_insert_id());
return 1;
}
Re: MySQL -- Free ID Slot -
iZN - 10.06.2014
No not like that! If you're having AUTO_INCREMENT, you don't have to specify VehicleID. It will auto increment if you insert the data.
pawn Код:
COMMAND:test(playerid, params[])
{
new query[500]; // what the hell 500?
mysql_query("INSERT INTO Vehicles (VehicleOwner) VALUES ('Bitch')");
return 1;
}
Re: MySQL -- Free ID Slot -
yoran765 - 10.06.2014
If you delete a vehicle from the database and you retype the command, it won't refill the deleted vehicle slot... It would just increase where it left off at.
Re: MySQL -- Free ID Slot -
iZN - 10.06.2014
Quote:
Originally Posted by yoran765
If you delete a vehicle from the database and you retype the command, it won't refill the deleted vehicle slot... It would just increase where it left off at.
|
If you need to delete the one vehicle field, you can simply change AUTO_INCREMENT value by doing (send this query through phpMyAdmin).
PHP код:
ALTER TABLE `table` AUTO_INCREMENT = '0'; /* change 0 to your last VehicleID (after removing one of them) */
The above query resets AUTO_INCREMENT value to 0.
Re: MySQL -- Free ID Slot -
yoran765 - 10.06.2014
After doing so it resets the value of A_I. But what if you have deleted id 2 and you now have id 1 and 3 the next id would be 2. typing the cmd would make the next id... 3 right?
Re: MySQL -- Free ID Slot -
iZN - 10.06.2014
Quote:
Originally Posted by yoran765
After doing so it resets the value of A_I. But what if you have deleted id 2 and you now have id 1 and 3 the next id would be 2. typing the cmd would make the next id... 3 right?
|
If you don't have ID 2 and you already have ID 1 and 3, it'll start from ID 4, not from ID 2 again because the value is already incremented.
Re: MySQL -- Free ID Slot -
yoran765 - 10.06.2014
Aha! Is there anyway to reset the value of AUTO_INCREMENT without using PHPMYADMIN?