Looping issue for MYSQL
#1

Hey all,

I'm creating a house system and for some reason when I first /createhouse when server starts it'll attempt to use the ID of the last house created therefore it throws a error in MYSQL. On the second attempt, it gets the correct "new" id.


Код:
[21:05:42] CMySQLHandler::Query(INSERT INTO houses (HouseID, Owner, PosX, PosY,PosZ,Interior,VirtualWorld) VALUES(4,'Shockey_HD', 3.959691, -718.891052, 6.190841,0,0)) - An error has occured. (Error ID: 1062, Duplicate entry '4' for key 'PRIMARY')

[21:05:47] >> mysql_query( Connection handle: 1 )

[21:05:47] CMySQLHandler::Query(INSERT INTO houses (HouseID, Owner, PosX, PosY,PosZ,Interior,VirtualWorld) VALUES(5,'Shockey_HD', 3.120903, -716.215087, 6.090567,0,0)) - Successfully executed.
Here is my CreateHouse command:

pawn Код:
CMD:createhouse(playerid,params[])
{
    new ID,String[128], Query[258],hInterior,Float:myPos[3];
    if(sscanf(params,"ud",ID,hInterior)) return SendClientMessage(playerid, COLOR_GREY,"Syntax: /createhouse [Owner ID] [Interior]");
    {
        for(new i = 1; i < MAX_HOUSES; i++)
        {
            SendClientMessage(playerid, -1, "testing");
            if(hData[i][Spawned] == 0)
            {
               
                GetPlayerPos(playerid,myPos[0],myPos[1],myPos[2]);
                hData[i][Spawned] = 1;
                hData[i][LocX] = myPos[0];
                hData[i][LocY] = myPos[1];
                hData[i][LocZ] = myPos[2];
                format(hData[i][Owner],32,"%s",GetName(ID));
                format(String, sizeof(String), "This house is owned by %s\nID: %d", hData[i][Owner],i);
                hData[i][hTextID] = Create3DTextLabel(String, COLOR_GREEN, hData[i][LocX],hData[i][LocY],hData[i][LocZ]+0.5,10.0, 0,1);
                hData[i][hPickupID] = CreatePickup(1273,1,hData[i][LocX],hData[i][LocY],hData[i][LocZ],0);
                hData[i][Spawned] = 1;

                format(Query, sizeof(Query), "INSERT INTO houses (HouseID, Owner, PosX, PosY,PosZ,Interior,VirtualWorld) VALUES(%d,'%s', %f, %f, %f,%d,%d)", i,GetName(ID),myPos[0],myPos[1],myPos[2],GetPlayerInterior(playerid),GetPlayerVirtualWorld(playerid));
                mysql_query(Query);
               
                break;
            }
        }
    }
    return 1;
}
Reply
#2

First of all, using outdated version of mysql plugin is not that good idea. I've seen dozen server crashes from mysql functions and there's absolutely no support. Using the latest version with threaded queries is by far better (also faster).

About the problem, you should set "HouseID" to auto increment and let it set the next value itself. Store the HouseID returned by mysql_insert_id to a variable and you'll update the house's data according to its unique ID. Few more things:

- You never check if the player (ID) is connected.
- Array indexes start from 0.
- "hInterior" is never used.
- Avoid declaring all the variables at the top but rather when needed.
- Use strcpy (strcat but resetting "destination") instead of format for copying strings - it's much faster.
- You set "Spawned" to 1 twice.

As long as you reload the houses when a house is deleted, then have a global variable and assign how many houses were loaded. When it is time to create a house, the loop is not needed and you got the variable as the next house (you should make sure though that if its value + 1 is equal to the MAX_HOUSES (size of hData), you should return an error that the limit was reached).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)