ExecuteSQLSelect: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (GetRegistryValue moved to GetRegistryQuery)
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Note_box|It is strongly recommended that you use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
This function retrieves one of more rows from the registry. Since the registry is powered by a SQLite database, this function automatically executes a ''SELECT'' query and returns the result as an object that can only be read by the [[GetRegistryRow]] function.
This function retrieves a value from the server's registry. This is data that is stored across sessions and can be read by any script. This is useful if you want to easily store a setting for your script.
 
The registry is stored globally and can be read and written to by all scripts in sessions. This is useful if you want to store data for your script.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string getRegistryValue ( string key )
int getRegistryValue ( object r, string tables, string fields, string conditions, int limit )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''key:''' The key under which the data you wish to retrieve was stored
*'''r:''' The result-object where the results are stored in.
*'''tables:''' The tables you want to query. No spaces allowed. Multiple tables should be separated by a comma (,).
*'''fields:''' The fields you want to query. No spaced allowed. Wildcard (*) allowed to query all fields in the table. Multiple fields should be separated by a comma (,).
 
===Optional Arguments===
*'''conditions:''' The conditions for the query. Multiple conditions should be separated by logical operators (AND, OR).
*'''limit:''' Maximum amount of rows to return.


===Returns===
===Returns===
Returns a string containing the value stored or ''false'' if no value is stored under the specified key.
Returns the amount of retrieved rows.


==Example==  
==Example==
This example keeps track of the largest number of players playing at once on the server and announces each time the record has been broken.
This example keeps track of the largest number of players playing at once on the server and announces each time the record has been broken.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 20: Line 27:
addEventHandler ( "onPlayerJoin", getRootElement(), "maxPlayerCounter" )
addEventHandler ( "onPlayerJoin", getRootElement(), "maxPlayerCounter" )
function maxPlayerCounter ( )
function maxPlayerCounter ( )
     mostPlayers = getRegistryValue ( "maxPlayerCounter.mostPlayers" ) -- get the previous record from the registry
     getRegistryValue ( res, "myscript_stats", "mostplayers", "", 1 ) -- get the mostPlayers result from our global stats row
    getRegistryRow ( res, 0 ) -- get the actual mostPlayers value from the result-object
     playerCount = getPlayerCount() -- get the number of players now
     playerCount = getPlayerCount() -- get the number of players now
     if ( mostPlayers == false or playerCount > mostPlayers ) -- see if there was a previous record and if there was, see if we've broken it
     if ( mostPlayers == false or playerCount > mostPlayers ) -- see if there was a previous record and if there was, see if we've broken it
         outputChatBox ( "New player count record: " .. playerCount .. " players!" ) -- display a message in the chat box
         outputChatBox ( "New player count record: " .. playerCount .. " players!" ) -- display a message in the chat box
         setRegistryValue ( "maxPlayerCounter.mostPlayers", playerCount ) -- store our new record
         setRegistryValue ( "myscript_stats", "mostplayers = " .. playerCount, "" ) -- store our new record
     end
     end
end
end

Revision as of 17:22, 22 September 2006

This function retrieves one of more rows from the registry. Since the registry is powered by a SQLite database, this function automatically executes a SELECT query and returns the result as an object that can only be read by the GetRegistryRow function.

The registry is stored globally and can be read and written to by all scripts in sessions. This is useful if you want to store data for your script.

Syntax

int getRegistryValue ( object r, string tables, string fields, string conditions, int limit )

Required Arguments

  • r: The result-object where the results are stored in.
  • tables: The tables you want to query. No spaces allowed. Multiple tables should be separated by a comma (,).
  • fields: The fields you want to query. No spaced allowed. Wildcard (*) allowed to query all fields in the table. Multiple fields should be separated by a comma (,).

Optional Arguments

  • conditions: The conditions for the query. Multiple conditions should be separated by logical operators (AND, OR).
  • limit: Maximum amount of rows to return.

Returns

Returns the amount of retrieved rows.

Example

This example keeps track of the largest number of players playing at once on the server and announces each time the record has been broken.

-- Trigger our function every time a player joins
addEventHandler ( "onPlayerJoin", getRootElement(), "maxPlayerCounter" )
function maxPlayerCounter ( )
    getRegistryValue ( res, "myscript_stats", "mostplayers", "", 1 ) -- get the mostPlayers result from our global stats row
    getRegistryRow ( res, 0 ) -- get the actual mostPlayers value from the result-object
    playerCount = getPlayerCount() -- get the number of players now
    if ( mostPlayers == false or playerCount > mostPlayers ) -- see if there was a previous record and if there was, see if we've broken it
        outputChatBox ( "New player count record: " .. playerCount .. " players!" ) -- display a message in the chat box
        setRegistryValue ( "myscript_stats", "mostplayers = " .. playerCount, "" ) -- store our new record
    end
end

See Also