MySQL Threaded
#1

I have problems understanding MySQL threaded querries.
I am trying to convert the code above from non threaded to threaded.
How to make this with threaded querries?
pawn Код:
new row[128];
    new field[2][32];

    mysql_fetch_row_format(row, "|");
    explode(row, field, "|");
    mysql_free_result();

    format(UserStats[playerid][Password], 32, "%s", field[0]);
    UserStats[playerid][Money] = strval(field[1]);

    GivePlayerMoney(playerid, UserStats[playerid][Money]);
Reply
#2

Ok, let's give you a quick example of loading data from a SELECT query using threaded MySQL query system.
pawn Код:
new query[256];
format(query, sizeof(query), "SELECT field1, field2, field3 FROM table WHERE field4='Whatever'");
mysql_tquery(connectionHandle, query, "LoadData", ""); //send the threaded query and load the results in the LoadData function, that has no parameters

// Somewhere in the gamemode

forward LoadData();
public LoadData()
{
    new rows, fields;
    cache_get_data(rows, fields, connectionHandle); //number of rows and fields
    //we could have used rows = cache_num_rows(); too
    if(rows) //if we found at least one result
    {
        for(new i = 0; i < rows; i++) //looping through all the results
        {
            new field1, Float:field2, field3[16]; //we got one int, one float and one char
            field1 = cache_get_row_int(i, 0, connectionHandle);
            field2 = cache_get_row_float(i, 1, connectionHandle);
            cache_get_row(i, 2, field3, connectionHandle, sizeof(field3));
           
            //go ahead and use the variables however you want
        }
    }
        else
        {
                //no results
        }
    return 1;
}
Functions used for this little example:
- https://sampwiki.blast.hk/wiki/MySQL/R33#mysql_tquery
- https://sampwiki.blast.hk/wiki/MySQL/R33#cache_get_data
- https://sampwiki.blast.hk/wiki/MySQL/R33#cache_get_row_int
- https://sampwiki.blast.hk/wiki/MySQL/R33..._get_row_float
- https://sampwiki.blast.hk/wiki/MySQL/R33#cache_get_row
Reply
#3

Thanks a lot for your helpful response but is it possible to make it without calling any function?
For example something like:
pawn Код:
new query[256];
format(query, sizeof(query), "SELECT field1, field2, field3 FROM table WHERE field4='Whatever'");
mysql_tquery(connectionHandle, query); //send the threaded query and load the results in the LoadData function, that has no parameters
new rows, fields;
cache_get_data(rows, fields, connectionHandle); //number of rows and fields
//we could have used rows = cache_num_rows(); too
if(rows) //if we found at least one result
{
    for(new i = 0; i < rows; i++) //looping through all the results
    {
    new field1, Float:field2, field3[16]; //we got one int, one float and one char
            field1 = cache_get_row_int(i, 0, connectionHandle);
            field2 = cache_get_row_float(i, 1, connectionHandle);
            cache_get_row(i, 2, field3, connectionHandle, sizeof(field3));
           
            //go ahead and use the variables however you want
        }
    }
        else
        {
                //no results
        }
Reply
#4

Yes, just do something like this:
pawn Код:
format(query, sizeof(query), "SELECT field1, field2, field3 FROM table WHERE field4='Whatever'");
mysql_tquery(connectionHandle, query, "", ""); //the two parameters are not optional, so they must be defined even if they are not used
Reply
#5

Uhm, are you sure you don't have to specify a callback when SELECTing stuff?

Also
pawn Код:
mysql_tquery(connectionHandle, query, "", ""); //the two parameters are not optional, so they must be defined even if they are not used
That's not true, you don't have to define if they are not used.
pawn Код:
mysql_tquery(connectionHandle, query);
EDIT: I became Huge clucker
Reply
#6

Quote:
Originally Posted by Sime30
Посмотреть сообщение
Uhm, are you sure you don't have to specify a callback when SELECTing stuff?

Also
pawn Код:
mysql_tquery(connectionHandle, query, "", ""); //the two parameters are not optional, so they must be defined even if they are not used
That's not true, you don't have to define if they are not used.
pawn Код:
mysql_tquery(connectionHandle, query);
EDIT: I became Huge clucker
Oh, right, it depends on the plugin version. I'm used to R34 and these parameters are not optional yet here. They became optional since R35.

Mike, if you are using BlueG R35 or later, just do what Sime30 said, that is correct for these versions.
Reply
#7

Quote:
Originally Posted by mike_1
Посмотреть сообщение
Thanks a lot for your helpful response but is it possible to make it without calling any function?
For example something like:
pawn Код:
new query[256];
format(query, sizeof(query), "SELECT field1, field2, field3 FROM table WHERE field4='Whatever'");
mysql_tquery(connectionHandle, query); //send the threaded query and load the results in the LoadData function, that has no parameters
new rows, fields;
cache_get_data(rows, fields, connectionHandle); //number of rows and fields
//we could have used rows = cache_num_rows(); too
if(rows) //if we found at least one result
{
    for(new i = 0; i < rows; i++) //looping through all the results
    {
    new field1, Float:field2, field3[16]; //we got one int, one float and one char
            field1 = cache_get_row_int(i, 0, connectionHandle);
            field2 = cache_get_row_float(i, 1, connectionHandle);
            cache_get_row(i, 2, field3, connectionHandle, sizeof(field3));
           
            //go ahead and use the variables however you want
        }
    }
        else
        {
                //no results
        }
I believe this is what you are trying to achieve.

https://sampforum.blast.hk/showthread.php?tid=548986

With inline you can have a function within a function. The tutorial above will show you how.
Reply
#8

Yes, using y_inline will make your script nicer.
But can you anwser me, do we have to specify a callback when SELECTing?
I know that you don't have to when UPDATing and INSERTing.

It's like something is telling me it's wrong doing SELECT without a callback :P
Reply
#9

I don't think it's wrong, but obviously a SELECT query most likely has an output, a cached result set. When you use a SELECT query, you expect an answer and you want to evaluate that answer from the database, so you have no reason to execute a SELECT query without processing the result.
Reply
#10

Thank you for the information.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)