Internal ID and MySQL auto increment ID
#1

Hi guys!

I had a working business system, buying, creating and everything! Now I edited something and it's all messed up!
How can I make that my Internal biz id is the same as MySQL autoincrement primary key? Its like in MySQL is 1, but in script starts as 0, as everything else.

Here are my codes...

pawn Код:
cmd:createbiz
{
new ID = spawnedbiz + 1;
            GetPlayerPos(playerid,xa,ya,za);
            FirmaInfo[ID][EXTX] = xa;
            FirmaInfo[ID][EXTY] = ya;
            FirmaInfo[ID][EXTZ] = za;
mysql_format(MySQLHandle, Query, sizeof(Query),"INSERT INTO  firme  ( name, position and stuff) VALUES (not important);
                        mysql_tquery(MySQLHandle, Query, "
OnBizCreate", "i", ID);
           
}

forward OnBizCreate(idbiz);
public OnBizCreate(idbiz)
{

    FirmaInfo[idbiz][IDBiz] = cache_insert_id();
    spawnedbiz ++;
    printf("
New biz - ID: %d", spawnedbiz ); // if is the first one created, it will write "1"
}


forward LoadBiz();
public LoadBiz()
{
        new Query[ 256 ];

        if(cache_num_rows())
        {
            for(new i, j = cache_num_rows(); i < j ; i++)
            {
                             FirmaInfo[i][IDBiz] = cache_get_field_content_int(i, "
IDBiz"); // This is the first field AKA autoincrement primary key in the table

                             ReloadBizLabels(i); // if i put here "
FirmaInfo[i][IDBiz]" it won't show the first one biz, well the only one when I created so I put it as "i"
                       
                        spawnedbiz ++;
                       
                        printf("
Debug id biz loadied is %d",FirmaInfo[i][IDBiz]); // "1"
Well you get the point... How to make them 1 Internal ID == 1 MYSQL ID, not 0 == 1
Reply
#2

The primary key's only purpose is to uniquely identify the row in the table, it should have no other meaning.

pawn Код:
FirmaInfo[i][IDBiz] = cache_get_field_content_int(i, "IDBiz");
You already have that. When referring to the biz in the script, use "i", when referring to the row in the database use "IDBiz".
Reply
#3

Also, when you want to set the ID yourself, remove the auto-increment from your ID column, but keep the primary key setting.
Then you can control how the ID is stored, and you can use the same ID in both your array as in MySQL.
When loading data from MySQL, You can use the ID as the index of your array. Read the ID first, then use that ID to store the rest of your data in the proper array-index upon reading it from the database.
When saving, do the same: use the index of your array as ID in your table.
Your database will have the same data with the same ID as in your script this way.

You'll need to add the ID to your INSERT query to add a new row to the database, as MySQL won't auto-increase it anymore.


I'm doing the same thing when creating gas-stations, police-stations, speedcams, houses, businesses, mission-locations, mission-data, mission vehicles (which are spawned near every spawnlocation), ...
All that data is stored inside arrays and is constantly used by the script from the moment you start the server until you shut it down (static data that has a preset limit, such as a limit for the max amount of houses, speedcams and such).

Only keep the auto-increment setting for data that isn't loaded fully upon OnGameModeInit such as logs and playerdata, as your server loads playerdata when they connect, they don't need to load data about all players when the server is started.
And logs aren't loaded at all, only during an admin-command perhaps to review the logs in-game.

Player-data and logs are dynamic data, they keep getting larger and larger without a preset limit, and they aren't stored inside arrays for the entire runtime duration of your server.
Reply
#4

Sorry to just take over this topic, but it's better than me making one. So, is it better to not use the auto increment when working with houses, businesses & vehicles?
Reply
#5

Thank you Vince and PPC!!!

I followed your suggestion PPC and here is how I edited it!


Note that I translate my stuff to english...

pawn Код:
BizInfo[i][IDBiz] = cache_get_field_content_int(i, "IDBiz"); // This is loaded first
// then are going other codes such as
BizInfo[i][Type] = cache_get_field_content_int(i, "Type"); // this was changed to
BizInfo[BizInfo[i][IDBiz][Type] = cache_get_field_content_int(i, "Type"); // this
Now, I changed all "i" to "BizInfo[i][IDBiz]"
It's working fine now, eggzactly as I wanted to

I have some questions.

Do I have to change the other "i" -> (i, "Type") or leave it as is?
Is that all I have to do? I did that thingy on inserting, updating is also fine.

@Luis - Trust me, if you don't want separate IDs , use this method!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)