PDA

View Full Version : MySQL Threaded


mike_1
06/02/2015, 02:23 PM
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?
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]);

HazardouS
06/02/2015, 02:38 PM
Ok, let's give you a quick example of loading data from a SELECT query using threaded MySQL query system.

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:
- http://wiki.sa-mp.com/wiki/MySQL/R33#mysql_tquery
- http://wiki.sa-mp.com/wiki/MySQL/R33#cache_get_data
- http://wiki.sa-mp.com/wiki/MySQL/R33#cache_get_row_int
- http://wiki.sa-mp.com/wiki/MySQL/R33#cache_get_row_float
- http://wiki.sa-mp.com/wiki/MySQL/R33#cache_get_row

mike_1
06/02/2015, 02:56 PM
Thanks a lot for your helpful response but is it possible to make it without calling any function?
For example something like:

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
}

HazardouS
06/02/2015, 03:02 PM
Yes, just do something like this:

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

Sime30
06/02/2015, 06:44 PM
Uhm, are you sure you don't have to specify a callback when SELECTing stuff?

Also

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.

mysql_tquery(connectionHandle, query);


EDIT: I became Huge clucker :D

HazardouS
06/02/2015, 06:55 PM
Uhm, are you sure you don't have to specify a callback when SELECTing stuff?

Also

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.

mysql_tquery(connectionHandle, query);


EDIT: I became Huge clucker :D

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.

Knappen
06/02/2015, 07:03 PM
Thanks a lot for your helpful response but is it possible to make it without calling any function?
For example something like:

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.

http://forum.sa-mp.com/showthread.php?t=548986

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

Sime30
06/02/2015, 07:30 PM
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

HazardouS
06/02/2015, 07:35 PM
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.

Sime30
06/02/2015, 07:41 PM
Thank you for the information.