AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Multiple queries (or queries queue) (https://forums.alliedmods.net/showthread.php?t=288766)

notMe2 10-07-2016 10:51

Multiple queries (or queries queue)
 
I'm trying to execute the code bellow, but the only the first query executes:
Code:

for(int i = 0; i <= MAXPLAYERS; i++)
{
    if(!isValidClient(i))
        continue;

    //query build

    SQL_TQuery(hDb, T_CommomQuery, sQuery, _, DBPrio_Normal);
}

Any ideas why?

shavit 10-07-2016 11:21

Re: Multiple queries (or queries queue)
 
not sure why exactly, but initialize the loop like this:
Code:

for(int i = 1; i <= MaxClients; i++)
client indexes start from 1 and looping until MAXPLAYERS is bad because it's a constant value (i think of 55); looping up to MaxClients is better because that's the biggest number that could be an entity index with your maxplayer setting

notMe2 10-07-2016 13:05

Re: Multiple queries (or queries queue)
 
Quote:

Originally Posted by shavit (Post 2460019)
not sure why exactly, but initialize the loop like this:
Code:

for(int i = 1; i <= MaxClients; i++)
client indexes start from 1 and looping until MAXPLAYERS is bad because it's a constant value (i think of 55); looping up to MaxClients is better because that's the biggest number that could be an entity index with your maxplayer setting

Hey shavit, thanks for your tips.

The usual game is only 5 vs. 5, i don't think that the size of loop is the problem. The problem is that only the first query is executed, the other 9 (in case of 5vs5) are ignored.

Kryptanyte 10-07-2016 15:02

Re: Multiple queries (or queries queue)
 
I'm not sure if I'm right about this but starting a query inside the for loop will break it (end the for loop) and wait for the query to respond.

You could try using transactions instead of doing each query individually - Transactions take several queries and execute them sequentially then pass results back to callbacks.

See this for more information: https://sm.alliedmods.net/new-api/dbi/Database/Execute

eg:
Code:

Transaction th_Queries = null;
th_Queries = SQL_CreateTransaction();
   
for(int i = 1; i < MaxClients; i++)
    SQL_AddQuery(th_Queries, /*Put your query here*/, /*Put your query data (if any) here*/);

SQL_Execute(hDb, th_Queries, SQL_QuerySuccess_Callback, SQL_QueryFailed_Callback);

Also don't think you can do threaded transaction queries so you will need to lock your database I would assume since you have to for regular queries, I'm not very familiar with threaded queries so if someone wants to correct me if I'm wrong please feel free

Mitchell 10-08-2016 01:23

Re: Multiple queries (or queries queue)
 
Also if you plan on passing a client id through the transaction make sure you convert it to userid then convert it back to index when the callback is called.
Transactions are the best way to catch all players instead of doing one by one.
I have an example of using two different queries to get player settings etc in my build mod:
Spoiler

notMe2 10-10-2016 10:52

Re: Multiple queries (or queries queue)
 
Thanks guys! both Mitchell and Kryptanyte solutions worked for me


All times are GMT -4. The time now is 20:04.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.