Mysql question
#1

Hello,

I have a table with around 400 vehicles.
All those vehicle have an ID.
Now is my question.
How can I set the ID variable that it will automaticly change to the correct ID.
So if I delete vehicle 399, vehicle 400 will be set to id 399.
I hope you get my point.

I wait for answer.
Reply
#2

This is my one here.
NOTE: if you just copy and paste this it will not work as we most likely have different names for our stuff. So just edit it around and get add you stuff in.
pawn Код:
stock ResetVehicleID()
{
    new Query[200];
    format(Query, sizeof(Query), "ALTER TABLE vehicles DROP id");
    mysql_query(Query);
    format(Query, sizeof(Query), "ALTER TABLE vehicles  ADD id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST");
    mysql_query(Query);
}
Reply
#3

Quote:
Originally Posted by Hayden_Bruin
Посмотреть сообщение
This is my one here.
NOTE: if you just copy and paste this it will not work as we most likely have different names for our stuff. So just edit it around and get add you stuff in.
pawn Код:
stock ResetVehicleID()
{
    new Query[200];
    format(Query, sizeof(Query), "ALTER TABLE vehicles DROP id");
    mysql_query(Query);
    format(Query, sizeof(Query), "ALTER TABLE vehicles  ADD id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST");
    mysql_query(Query);
}
With this code, you will need to also re-load the vehicles variables if you use the 'id' in-game. If you don't you will fuck it up pretty bad when trying to save (if you use id for that too).
Reply
#4

This is the function I made for this:
pawn Код:
stock GetNextEmptyHouseID()
{
    new result = 1, id = 0;
    while(result != 0)
    {
        id++;
        format(Query, sizeof(Query), "SELECT * FROM `Houses` WHERE HouseID = '%d'", id);
        mysql_query(Query);
        mysql_store_result();
        result = mysql_num_rows();
    }
    mysql_free_result();
    return id;
}
This function is used for my house system, but you can easily change it.
Reply
#5

Quote:
Originally Posted by Hayden_Bruin
Посмотреть сообщение
This is my one here.
NOTE: if you just copy and paste this it will not work as we most likely have different names for our stuff. So just edit it around and get add you stuff in.
pawn Код:
stock ResetVehicleID()
{
    new Query[200];
    format(Query, sizeof(Query), "ALTER TABLE vehicles DROP id");
    mysql_query(Query);
    format(Query, sizeof(Query), "ALTER TABLE vehicles  ADD id INT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST");
    mysql_query(Query);
}
So this code will set order all id's?
Exampe: 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10
I delete vehicle id 4.
1 , 2, 3, 5, 6, 7, 8, 9, 10
After this function it will be
1 , 2, 3, 4, 5, 6, 7, 8, 9,

Edit: I tested it, and it works.
So now i will add something like a random VehicleNumber for every vehicles.
I will load the vehicles on the ID's and i will save them on the VehicleNumber
I hope works
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)