Loading SQL Vehicles
#1

Hey, I'm trying to load vehicles from SQL, but am having some difficulty.
I would just like to first say that I had vehicles loading fine before, using a for loop, though that was really slow and wasn't using SQL to it's full potential.
I've been looking for answers for quite some time now, but I couldn't find anything specific enough to help me out.

pawn Код:
format(sqlQuery, sizeof(sqlQuery), "SELECT * FROM samp_vehicles");
                    mysql_query(sqlQuery, THREAD_LOADCARS);
This returns
Quote:

Runtime Error 4: "Array index out of bounds"
Accessing element at negative index -1
Backtrace (most recent call first):
#0 public OnQueryFinish()+0x36beac from gm.amx

pawn Код:
format(sqlQuery, sizeof(sqlQuery), "SELECT * FROM 'samp_vehicles'");
                    mysql_query(sqlQuery, THREAD_LOADCARS);
This returns on the mysql.log
Quote:

(12/4/2012)[16:51:7] error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''samp_vehicles'' at line 1 | query:

under OnQueryFinish I got
pawn Код:
mysql_store_result();
            new row[4000];
            while(mysql_fetch_row(row))
            {
                sscanf(row, "p<|>e<ddffffdds[64]ds[20]ddddddddddddddddddds[64]d>", CarInfo[extraid]);
                ownedcar[extraid] = AddStaticVehicle(CarInfo[extraid][cModel],CarInfo[extraid][cLocationx],CarInfo[extraid][cLocationy],CarInfo[extraid][cLocationz], CarInfo[extraid][cColorOne], CarInfo[extraid][cColorTwo],100);   
                if(CarInfo[extraid][cPaintjob] != 999)
                {
                    ChangeVehiclePaintjob(extraid, CarInfo[extraid][cPaintjob]);
                }
                SetVehicleVirtualWorld(extraid, CarInfo[extraid][cVirWorld]);
                SetVehicleNumberPlate(extraid, CarInfo[extraid][cPlate]);
                SetVehicleModifications(extraid);
            }
            mysql_free_result();
I'm wanting to try without the e<>, though it is too long and I'm unsure of how to break it up. First time using sscanf as well as a while loop for SQL.

any help is greatly appreciated .
Reply
#2

Page 8 bump. I haven't been able to find a fix that didnt just result in errors.
Reply
#3

Try not using OnQueryFinish at all. Just use
pawn Код:
mysql_store_result();
            new row[4000];
            while(mysql_fetch_row(row))
            {
                sscanf(row, "p<|>e<ddffffdds[64]ds[20]ddddddddddddddddddds[64]d>", CarInfo[extraid]);
                ownedcar[extraid] = AddStaticVehicle(CarInfo[extraid][cModel],CarInfo[extraid][cLocationx],CarInfo[extraid][cLocationy],CarInfo[extraid][cLocationz], CarInfo[extraid][cColorOne], CarInfo[extraid][cColorTwo],100);    
                if(CarInfo[extraid][cPaintjob] != 999)
                {
                    ChangeVehiclePaintjob(extraid, CarInfo[extraid][cPaintjob]);
                }
                SetVehicleVirtualWorld(extraid, CarInfo[extraid][cVirWorld]);
                SetVehicleNumberPlate(extraid, CarInfo[extraid][cPlate]);
                SetVehicleModifications(extraid);
            }
            mysql_free_result();
After the mysql_query.

pawn Код:
format(sqlQuery, sizeof(sqlQuery), "SELECT * FROM samp_vehicles");
mysql_query(sqlQuery, THREAD_LOADCARS);
That is correct though . Lastly, what is THREAD_LOADCARS defined as?
Reply
#4

Alright it's working now, though only loading the first car in the DB, and for some reason the plate doesn't change(the second string).
EDIT: I also added another sscanf to set the carid array.

pawn Код:
new sqlQuery[164];
    format(sqlQuery, sizeof(sqlQuery), "SELECT * FROM samp_vehicles");
    mysql_query(sqlQuery);
    mysql_store_result();
    new row[4000], carid;
    while(mysql_fetch_row(row))
    {
        sscanf(row, "p<|>d",carid);
        sscanf(row, "p<|>e<ddffffdds[64]ds[16]ddddddddddddddddddds[64]d>", CarInfo[carid]);
        ownedcar[carid] = AddStaticVehicle(CarInfo[carid][cModel],CarInfo[carid][cLocationx],CarInfo[carid][cLocationy],CarInfo[carid][cLocationz], CarInfo[carid][cColorOne], CarInfo[carid][cColorTwo],100)
        if(CarInfo[carid][cPaintjob] != 999)
        {
            ChangeVehiclePaintjob(carid, CarInfo[carid][cPaintjob]);
        }
        SetVehicleVirtualWorld(carid, CarInfo[carid][cVirWorld]);
        SetVehicleNumberPlate(carid, CarInfo[carid][cPlate]);
        SetVehicleModifications(carid);
    }
    mysql_free_result();
THREAD_LOADCARS was just being defined as "10" since OnQueryFinish was using Case.

EDIT2: Might as well show the enum.
pawn Код:
enum cInfo
{
    cID,
    cModel,
    Float:cLocationx,
    Float:cLocationy,
    Float:cLocationz,
    Float:cAngle,
    cColorOne,
    cColorTwo,
    cOwner[MAX_PLAYER_NAME],
    cOwned,
    cPlate[15],
    cLock,
    cPaintjob,
    cVirWorld,
    cUnused,
    cComponent0,
    cComponent1,
    cComponent2,
    cComponent3,
    cComponent4,
    cComponent5,
    cComponent6,
    cComponent7,
    cComponent8,
    cComponent9,
    cComponent10,
    cComponent11,
    cComponent12,
    cComponent13,
    cCode,
    cTicketer[MAX_PLAYER_NAME],
    cParkTicket
};

new CarInfo[MAX_VEHICLES][cInfo];
EDIT3: Made a quick command to list the cInfo for car 1(the only car from the DB that spawns) and cPlate is returning nothing. Hmm
EDIT4: Took another look at the command results and everything after the first string, which is Owner, is incorrect.
EDIT5: Took yet another look, it is skipping cOwned, cPlate, cLock, cPaintjob, cVirWorld, cUnused. cComponent0 - cComponent13 is fine. Skips cCode, cTicketer, and sets cParkTicket to the integer in cUnused.
Reply
#5

pawn Код:
sscanf(row, "p<|>d",carid);
That won't work as the row isn't just one single ID. I would recommend trying this:

pawn Код:
mysql_query("SELECT * FROM samp_vehicles");
mysql_store_result();
new row[4000], carid;
while(mysql_fetch_row(row))
{
        carid ++;
        sscanf(row, "p<|>e<ddffffdds[64]ds[16]ddddddddddddddddddds[64]d>", CarInfo[carid]);
        ownedcar[carid] = AddStaticVehicle(CarInfo[carid][cModel],CarInfo[carid][cLocationx],CarInfo[carid][cLocationy],CarInfo[carid][cLocationz], CarInfo[carid][cColorOne], CarInfo[carid][cColorTwo],100);  
        if(CarInfo[carid][cPaintjob] != 999)
        {
            ChangeVehiclePaintjob(carid, CarInfo[carid][cPaintjob]);
        }
        SetVehicleVirtualWorld(carid, CarInfo[carid][cVirWorld]);
        SetVehicleNumberPlate(carid, CarInfo[carid][cPlate]);
        SetVehicleModifications(carid);
}
mysql_free_result();
You don't really need the carid's to match up because you always have the carid stored in the enum anyway. Give that a go and see what happens.
Reply
#6

Still only loading the first vehicle as well as the following.
skips cOwned, cPlate, cLock, cPaintjob, cVirWorld, cUnused.
cComponent0 - cComponent13 is fine.
Skips cCode, cTicketer, and sets cParkTicket to the integer in cVirWorld.


Also, just so you know there are 977 vehicles in the DB.
Reply
#7

In the sscanf line there is 32 values, the enum has 31 variables, double check that. For fixing the only loading the first vehicle issue, do this instead:

pawn Код:
mysql_query("SELECT * FROM samp_vehicles");
mysql_store_result();
new row[4000], carid;
while(mysql_retrieve_row())
{
        mysql_fetch_row(row);
        carid ++;
        sscanf(row, "p<|>e<ddffffdds[64]ds[16]ddddddddddddddddddds[64]d>", CarInfo[carid]);
        ownedcar[carid] = AddStaticVehicle(CarInfo[carid][cModel],CarInfo[carid][cLocationx],CarInfo[carid][cLocationy],CarInfo[carid][cLocationz], CarInfo[carid][cColorOne], CarInfo[carid][cColorTwo],100);  
        if(CarInfo[carid][cPaintjob] != 999)
        {
            ChangeVehiclePaintjob(carid, CarInfo[carid][cPaintjob]);
        }
        SetVehicleVirtualWorld(carid, CarInfo[carid][cVirWorld]);
        SetVehicleNumberPlate(carid, CarInfo[carid][cPlate]);
        SetVehicleModifications(carid);
}
mysql_free_result();
Reply
#8

I checked the enum and sscanf. enum has 32 variables, sscanf has 32 variables.

With this new attempt, it's skipping the first vehicle in the DB and loading only the second. Still skipping those variables listed above as well. Also seems to be setting those fields in the DB that are being skipped to 0/blank the first time.

I noticed it did that for the first car before, so I manually changed the fields in the DB back to what they were before and then restarted the server. It didn't change them back to 0/blank, but it didn't load them still.
It's now doing that for this second car that has now loaded.

I'm so confused.
Reply
#9

Ahh, I must have miscounted the enum, can you put the following:
pawn Код:
print(row);
Then paste the value of that here? Also, make sure that you have the most updated version of sscanf to your server version.
Reply
#10

I edited the post above your reply so I'll paste what I said in case you didnt notice.
Quote:

Still skipping those variables listed above as well. Also seems to be setting those fields in the DB that are being skipped to 0/blank the first time.

I noticed it did that for the first car before, so I manually changed the fields in the DB back to what they were before and then restarted the server. It didn't change them back to 0/blank, but it didn't load them still.
It's now doing that for this second car that has now loaded.

I'm so confused.

As for the print, it's returning the entire row.
2|580|1378.6|-773.023|94.587|351.335|0|0|Joao_Caldeira|0||0|0|0| 0|1023|20|30|40|50|1010|1018|1082|60|1087|70|80|90 |100|0||0

It set owned to 0, locked to 0, plate to blank, code to 0, paintjob to 0, possibly setting ticketer to blank, possibly setting parkticket to 0, possibly setting virtual world to 0, possibly setting unused to 0, and set all the components to what was in the first row. (So, everything after Owner is being changed).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)