Difference between tquery and query
#1

Hi everyone,
I've already read a few topic about it, but I wish to know what it happens when I execute, for example, this code:

Code:
mysql_format(MySQL, query, sizeof(query), "SELECT * FROM `players` WHERE `Name` = '%s' LIMIT 1", pname);
mysql_tquery(MySQL, query, "OnPlayerDataLoaded", "d", playerid);

SetPlayerHealth(playerid,PlayerData[playerid][pHealth]);
Here OnPlayerDataLoaded:
Code:
forward OnPlayerDataLoaded(playerid);
public OnPlayerDataLoaded(playerid)
{
        PlayerData[playerid][pHealth] = cache_get_field_content_float(0,"Health");
	return 1;
}
When i call SetPlayerHealth function, has PlayerData[playerid][pHealth] been updated with the new value?
Or what is the proper way to use tquery?

Thanks and regards.
Reply
#2

You can simply use it in the OnPlayerDataLoaded callback.

PHP Code:
forward OnPlayerDataLoaded(playerid);
public 
OnPlayerDataLoaded(playerid)
{
        
PlayerData[playerid][pHealth] = cache_get_field_content_float(0,"Health");

        
SetPlayerHealth(playerid,PlayerData[playerid][pHealth]);
    return 
1;

Reply
#3

So, is advisable use tquery instead query?
Reply
#4

As far i know the "t" there means threaded query so it runs on another thread, the PlayerData[playerid][pHealth] isn't likely going to be updated because SetPlayerHealth(playerid,PlayerData[playerid][pHealth]) may be already called first because they are not synchronized to each other, unless you place them like oma37's code.

use things like this if you still want to use tquery like the way you explained:
https://sampforum.blast.hk/showthread.php?tid=548986
Reply
#5

"Normal" non-threaded queries are blocking; nothing gets done until a response is received from the MySQL server. That includes basic things like player sync. The average query takes about 10 milliseconds to execute but these times add up fast, and as the database grows larger so too will execution time. More complex queries can take a few seconds to execute.

Threaded queries are, like the name implies, executed in another thread separate from that of the SA-MP server so nothing is affected. Therefore it is beneficial to use threaded queries whenever they're viable. The result isn't immediately available, which is kind of the point. It's more or less comparable to setting a timer.
Reply
#6

Now i understand, but when tquery could be useful in my gamemode?
Reply
#7

Quote:
Originally Posted by pollo97
View Post
Now i understand, but when tquery could be useful in my gamemode?
Look at what Vince said, it will be useful when the query is complex.

For example, you are loading 500 data of each player, consider using tquery for this case, because if you use a non threaded query, it will wait till the loading process gets done which took so long and may make your server lag. With a tquery, it "runs in background" without annoying/blocking the other process you have in your script.
Reply
#8

i have a question dont want to create thread as isn't really neccassary:
like i want to create a column to hold Cache of player what will be its type...as there is not "cache" type in phpmyadmin...
PS:never mind i asked a dumb question...figure it out its not about cache...it was something else i was doing wrong and started to blame Caches lol :P
Reply
#9

I always use tquery and use query only and only if I really need that information from to be loaded before I continue. One example is when you ban someone and you have a custom ban function.

1. Anti cheat finds out that someone is let's say flying around.
2. Anti cheat will ban the player and create a row that the player is banned in an entity in the DB.
3. The anti cheat continues to ban that player again because first ban hasn't really been done yet.

The result here is that you might find yourself having either multiple rows or in the most probable scenario if you built the DB entity correctly, you will have admin spam that a player has been banned around 11 times. So in this case I used query instead so that, that particular code wouldn't continue until it is fully done, and the spamming was stopped (the example here is not entirely as "complex" as what actually happened but is summarized to not create too much content to prove a point ).

Try to always use tquery (out of total 335 I use 5 "mysql_query") , as others also have pointed out, but just wanted to highlighten how useful this function is. I wish we could create threads and it being safe to use them.

EDIT: Didn't see the newest post my bad. To answer that is that "cache" isn't a type. I believe it's a tag in pawn but it's not really how you get the data. Your question for me is not too clear, but if you were wondering how to receive data it's by
Code:
cache_get_field_content_int(row, "name_of_column");
cache_get_field_content_float(row, "name_of_column");
cache_get_field_content(row, "name_of_column", String_to_place_it_in, Your_Connection, sizeof(String_to_place_it_in));
I'm sure there are more but these are the most common ones used in pawn.
Reply
#10

Quote:
Originally Posted by princejeet1510
View Post
i have a question dont want to create thread as isn't really neccassary:
lol uhm, better make a separated thread.
Quote:
Originally Posted by princejeet1510
View Post
like i want to create a column to hold Cache of player what will be its type...as there is not "cache" type in phpmyadmin...
cache is just actually a normal data that is being stored temporary so it can be loaded or speed up process without having to do same thing everytime where you don't really need to, so it's technical thing which depends on how you script them, not some kind of data type.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)