ExecuteSQLSelect: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Changed "DeprecatedWithAlt" template to "Deprecated")
 
(31 intermediate revisions by 11 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__
{{Note_box|It is strongly reccomended that you use the standard ''module.key'' naming for your keys, as shown in the example below. This prevents collisions between different scripts.}}
{{Server 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.
{{Deprecated|executeSQLQuery|See the examples at executeSQLQuery for equivalent SELECT usage.}}
 
This function retrieves rows from a table in the database, if they exist. If you pass the table name, along with the columns you want to retrieve (and any conditions for the row) this function will return a table containing the corresponding values.
 
The SQLite database contains globally stored data and can be used by scripts to store and retrieve data in a structured manner.
 
The executed SQL query is the following:
 
<syntaxhighlight lang="lua">[sql]SELECT <fields> FROM <tables> WHERE <conditions> LIMIT <limit></syntaxhighlight>


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string getRegistryValue ( string key )
table executeSQLSelect ( string tableName, 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
*'''tableName:''' The table you want to query. No spaces allowed.
*'''fields:''' The fields you want to query. No spaces allowed. Multiple fields should be separated by a comma (,). Wildcard (*) allowed to query all fields in the table. If you use wildcards, please pay attention to the order in which you'll have to retrieve your values from the table.
 
===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 a 2-dimensional table where the results are stored as table [row_index] [column_name]. Please note that table may be empty.
 
If invalid arguments were passed, returns ''false''.


==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 creates a SQL table when a map loads, and stores info about a player to that database when he spawns.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onPlayerJoin", getRootElement(), "maxPlayerCounter" )
function onMapLoad ()
function maxPlayerCounter ( )
-- create our table, if it doesn't already exist
    mostPlayers = getRegistryValue ( "maxPlayerCounter.mostPlayers" )
executeSQLCreateTable ( "players", "clothes_head_texture TEXT, clothes_head_model TEXT, player TEXT" )
    playerCount = getPlayerCount()
end
    if ( mostPlayers == false or playerCount > mostPlayers )
addEventHandler ( "onGamemodeMapStart", getRootElement(), onMapLoad )
        outputChatBox ( "New player count record: " .. playerCount .. " players!" )
 
        setRegistryValue ( "maxPlayerCounter.mostPlayers", playerCount )
function addInfoToSQL( theSpawnpoint, theTeam )
    end
sourcename = getPlayerName ( source ) -- get the player's name
-- try to retrieve the player data from the db
result = executeSQLSelect ( "players", "player", "player = '" .. sourcename .. "'" )
if ( type( result ) == "table" and #result == 0 ) or not result then -- see if any data was found at all
outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source )
executeSQLInsert ( "players", "'none', 'none', '" .. sourcename .. "'" )
else
outputChatBox ( "Welcome back " .. sourcename .. "!", source )
executeSQLUpdate ( "players", "clothes_head_texture = 'hairgreen', clothes_head_model = 'somehead'",
"player = '" .. sourcename .. "'" )
end
-- get the clothes data for the player
result = executeSQLSelect ( "players", "clothes_head_texture, clothes_head_model", "player = '" .. sourcename .. "'" )
outputChatBox ( "Your head texture is " .. result[1].clothes_head_texture )
outputChatBox ( "Your head model is " .. result[1]['clothes_head_model'] )
end
end
addEventHandler ( "onPlayerSpawn", getRootElement(), addInfoToSQL )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Registry_functions}}
{{Registry_functions}}
[[ru:executeSQLSelect]]

Latest revision as of 16:20, 13 February 2015

Emblem-important.png This function is deprecated. This means that its use is discouraged and that it might not exist in future versions.

Please use executeSQLQuery instead. See the examples at executeSQLQuery for equivalent SELECT usage.


This function retrieves rows from a table in the database, if they exist. If you pass the table name, along with the columns you want to retrieve (and any conditions for the row) this function will return a table containing the corresponding values.

The SQLite database contains globally stored data and can be used by scripts to store and retrieve data in a structured manner.

The executed SQL query is the following:

[sql]SELECT <fields> FROM <tables> WHERE <conditions> LIMIT <limit>

Syntax

table executeSQLSelect ( string tableName, string fields, [ string conditions, int limit ] )

Required Arguments

  • tableName: The table you want to query. No spaces allowed.
  • fields: The fields you want to query. No spaces allowed. Multiple fields should be separated by a comma (,). Wildcard (*) allowed to query all fields in the table. If you use wildcards, please pay attention to the order in which you'll have to retrieve your values from the table.

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 a 2-dimensional table where the results are stored as table [row_index] [column_name]. Please note that table may be empty.

If invalid arguments were passed, returns false.

Example

This example creates a SQL table when a map loads, and stores info about a player to that database when he spawns.

function onMapLoad ()
	-- create our table, if it doesn't already exist
	executeSQLCreateTable ( "players", "clothes_head_texture TEXT, clothes_head_model TEXT, player TEXT" )
end
addEventHandler ( "onGamemodeMapStart", getRootElement(), onMapLoad )

function addInfoToSQL( theSpawnpoint, theTeam )	
	sourcename = getPlayerName ( source )	-- get the player's name
	
	-- try to retrieve the player data from the db
	result = executeSQLSelect ( "players", "player", "player = '" .. sourcename .. "'" )
	if ( type( result ) == "table" and #result == 0 ) or not result then -- see if any data was found at all
		outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source )
		executeSQLInsert ( "players", "'none', 'none', '" .. sourcename .. "'" )
	else
		outputChatBox ( "Welcome back " .. sourcename .. "!", source )
		executeSQLUpdate ( "players", "clothes_head_texture = 'hairgreen', clothes_head_model = 'somehead'",
		"player = '" .. sourcename .. "'" )
	end	
	
	-- get the clothes data for the player
	result = executeSQLSelect ( "players", "clothes_head_texture, clothes_head_model", "player = '" .. sourcename .. "'" )
	outputChatBox ( "Your head texture is " .. result[1].clothes_head_texture )
	outputChatBox ( "Your head model is " .. result[1]['clothes_head_model'] )	
end
addEventHandler ( "onPlayerSpawn", getRootElement(), addInfoToSQL )

See Also