AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved Clarify docs: What is multiple result set for SQL_LockDatabase()? (https://forums.alliedmods.net/showthread.php?t=333127)

Dragokas 06-20-2021 16:52

Clarify docs: What is multiple result set for SQL_LockDatabase()?
 
Hi,

can you clarify, please, this statement from docs:

Quote:

If your query returns multiple result sets, for example, a procedure call on MySQL that returns results, you must lock both the query and the entire fetch operation. SourceMod is only able to fetch one result set at a time, and all result sets must be cleared before a new query is started.
Can you show / explain example of such multiple result set?

Sample from docs:

PHP Code:

bool GetByAge_Query(Database dbint age)
{
    
char query[100];
    
FormatEx(querysizeof(query), "SELECT name FROM users WHERE age = %d"age);
 
    
SQL_LockDatabase(db);
 
    
DBResultSet hQuery SQL_Query(dbquery);
    if (
hQuery == null)
    {
        
SQL_UnlockDatabase(db);
        return 
false;
    }
 
    
SQL_UnlockDatabase(db);
 
    
PrintResults(hQuery);
 
    
delete hQuery;
 
    return 
true;
}

void PrintResults(Handle query)
{
    
/* Even if we have just one row, you must call SQL_FetchRow() first */
    
char name[MAX_NAME_LENGTH];
    while (
SQL_FetchRow(query))
    {
        
SQL_FetchString(query0namesizeof(name));
        
PrintToServer("Name \"%s\" was found."name);
    }


Do I correctly understand, if SQL_Query returns more than one row (such a way several calls to SQL_FetchRow will be required), in such case I should also move SQL_UnlockDatabase below the PrintResults?

asherkin 06-21-2021 03:46

Re: Clarify docs: What is multiple result set for SQL_LockDatabase()?
 
Each SQL query can return one or more result sets that each contain zero or more rows.

Only procedure calls can return multiple result sets with SM (as it says in the doc you quoted, since it doesn’t support using a semicolon to run multiple queries), so 99% of code doesn’t need to care about them and only needs to iterate over the rows of the first result set which is loaded automatically (as your code is doing).

There are separate MoreResults and FetchResult for additionally iterating over result sets.

Dragokas 06-21-2021 12:47

Re: Clarify docs: What is multiple result set for SQL_LockDatabase()?
 
Good to know. Thanks for the explanation!


All times are GMT -4. The time now is 14:49.

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