I need an faster way for this ..
#1

Right now i'm using the code below to load cars from mysql , but it takes ages to load ( ~ 1 minute for 100cars)
pawn Код:
public LoadVehicle(sqlvehicleid)
{
    new query[128];
    new row[32];
    new vid;
    new Float:cx, Float:cy, Float:cz, Float:az;
    /// VehModel ///
    format(query, sizeof(query), "SELECT `Model` FROM `vehicles` WHERE `id` = %d LIMIT 1", sqlvehicleid);
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(row);
    vid= strval(row);
    /// CX ///
    format(query, sizeof(query), "SELECT `Cx` FROM `vehicles` WHERE `id` = %d LIMIT 1", sqlvehicleid);
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(row);
    cx = strval(row);
    /// Cy ///
    format(query, sizeof(query), "SELECT `Cy` FROM `vehicles` WHERE `id` = %d LIMIT 1", sqlvehicleid);
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(row);
    cy = strval(row);
    /// Cz ///
    format(query, sizeof(query), "SELECT `Cz` FROM `vehicles` WHERE `id` = %d LIMIT 1", sqlvehicleid);
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(row);
    cz = strval(row);
    /// Inc ///
    format(query, sizeof(query), "SELECT `Inc` FROM `vehicles` WHERE `id` = %d LIMIT 1", sqlvehicleid);
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(row);
    az = strval(row);
    /// PRINTING ALL //
    printf("SERVER:AddStaticVehicle(%d,%f,%f,%f,%f,-1,-1)",vid,cx,cy,cz,az);
    AddStaticVehicle(vid,cx,cy,cz,az,-1,-1);
    totalveh++;

}
Is there an faster way of doing this ? like getting the whole row and spliting it ?
I need an exaple please .
Reply
#2

First up you should SELECT a whole row, then get the fields by using samp_mysql_get_field or by splitting the row like you've already suggested.

I was also going to suggest loading all your vehicles in one statement (removing the limit) but it looks like your SQL plugin doesn't have a next_row() type of function (assuming you're using "SAMP-MySQL v0.15" going by your function names?)

edit:

Was reading the docs for it again and it seems that continously using samp_mysql_fetch_row() may change row each time you use it. If that's true then you can do something like this after you've selected all vehicle data:

pawn Код:
for (new i = 0, j = samp_mysql_num_rows(); i < j; i++)
{
    samp_mysql_fetch_row(line) // get the row..
    // split it and do whatever you fancy
}
or

pawn Код:
for (new i = 0; samp_mysql_fetch_row(line); i++)
{
    // split the row and do what you fancy. I've left the i counter in
    // because you may need it for an array index counter (you can simply
    // turn this into a while loop if you don't need it)
}
Reply
#3

Give me an example please , so far i got this ( for the login system)
pawn Код:
public OnPlayerLogin(playerid,password[])
{
    new query[128];
    new Data[1024];
    new Field[64];
    new rcnt = 1;
    format(query, sizeof(query), "SELECT * FROM `players` WHERE `Name` = '%s' LIMIT 1", "14aa41");
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(Data);
    printf(" %s",Data);
    samp_mysql_strtok(Field, "|", Data);
    printf(" %s",Data);
    while (samp_mysql_strtok(Field, "|", "")==1)
    {
        if (rcnt == 5) PlayerInfo[playerid][AdminLevel] = strval(Field);
        printf("admin lvl %d",PlayerInfo[playerid][AdminLevel]);
        rcnt++;
        return 1;
    }
    return 1;
}
It doesn't work properly , i don't know why
Код:
[02:49:49] [join] 14aa41 has joined the server (0:127.0.0.1)
[02:50:09]  1|14aa41|123|30000|3
[02:50:09]  1|14aa41|123|30000|3
[02:50:09] admin lvl 0
[02:51:07]  1|14aa41|123|30000|3
[02:51:07]  1|14aa41|123|30000|3
[02:51:07] admin lvl 0
[02:51:08]  1|14aa41|123|30000|3
[02:51:08]  1|14aa41|123|30000|3
[02:51:08] admin lvl 0
[02:51:09]  1|14aa41|123|30000|3
[02:51:09]  1|14aa41|123|30000|3
[02:51:09] admin lvl 0
Reply
#4

I use SQLite (concepts are the same) so I may not be the best person to write the 'working' code, only confident with the theory side. However I will see what I can do.

After seeing your edit it appears that you're using the strtok incorrectly.

edit: Jeepers, that strtok is scary.. you should be able to use samp_mysql_get_field which would be a lot easier for you and probably more efficient.

pawn Код:
format(query, sizeof(query), "SELECT * FROM `players` WHERE `Name` = '%s' LIMIT 1", "14aa41");
    samp_mysql_query(query);
    samp_mysql_store_result();
    samp_mysql_fetch_row(Data);

    // Here's the pudding, try this instead of strtok and loop :p
    new
        intBuf[64];

    samp_mysql_get_field("<enter_the_name_of_admin_level_field_here>", intBuf);
    print("The admin level is %d", strval(intBuf));
Reply
#5

Solved , thanx alot Simon , your code work perfectly , im going to do the same with the vehicles .
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)