ExecuteSQLUpdate: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (SetRegistryValue moved to SetRegistryQuery)
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 sets one or more rows in the registry. Since the registry is powered by a SQLite database, this function automatically executes an ''UPDATE'' query with the appropriate parameters.  
This function sets a value in 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 from and written to by all scripts in sessions. This is useful if you want to store data for your script.
 
The actual SQL query that is executed will be the following:
<syntaxhighlight lang="lua">
UPDATE <table> SET <set> WHERE <where>
</syntaxhighlight>


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string setRegistryValue ( string key, [ string value ] )
bool setRegistryQuery ( string table, string set, [ string where ] )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''key:''' The key under which the data you wish to retrieve was stored
*'''table:''' The table you want to modify.
*'''set:''' The query you want to execute on that table.


===Optional Arguments===
===Optional Arguments===
*'''value:''' A [[string]] containing the value you wish to store in the registry. If this value is not passed, the registry key specified will be removed.
*'''where:''' The conditions that need to be met before a specific row is changed.


===Returns===
===Returns===
If a ''value'' is specified, returns ''true'' in all cases. If a ''value'' is not specified, returns ''true'' if the ''key'' specified existed (and has been removed), ''false'' if it did not exist.
The function returns a ''boolean'' which is ''true'' on success, and ''false'' on failure.


==Example==  
==Example==  
Line 23: Line 30:
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
-- Add a command handler called reset_max. This should in practice have some checks for a user's access level.
addCommandHandler ( "reset_max", "resetMaxPlayerCounter" )
function resetMaxPlayerCounter ( )
    setRegistryValue ( "maxPlayerCounter.mostPlayers" ) -- remove the registry key containing the max players record
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 17:28, 22 September 2006

This function sets one or more rows in the registry. Since the registry is powered by a SQLite database, this function automatically executes an UPDATE query with the appropriate parameters.

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

The actual SQL query that is executed will be the following:

UPDATE <table> SET <set> WHERE <where>

Syntax

bool setRegistryQuery ( string table, string set, [ string where ] )

Required Arguments

  • table: The table you want to modify.
  • set: The query you want to execute on that table.

Optional Arguments

  • where: The conditions that need to be met before a specific row is changed.

Returns

The function returns a boolean which is true on success, and false on failure.

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