ExecuteSQLInsert: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (AddRegistryQuery moved to ExecuteSQLInsert)
No edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
This function adds a row in the registry. Since the registry is powered by a SQLite database, this function automatically executes an ''INSERT'' query with the appropriate parameters. This function can be used to add rows into the database that can later be modified by using the [[setRegistryQuery]] function.
This function adds a row in the database.


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 SQLite database contains globally stored data and can be used by scripts to store and retrieve data in a structured manner.


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


<pre>INSERT INTO <table> VALUES <values></pre>
<syntaxhighlight lang="lua">[sql]INSERT INTO <table> VALUES <values></syntaxhighlight>


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool addRegistryQuery ( string table, string values )
bool executeSQLInsert ( string table, string values )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''table:''' The table you want to modify.
*'''table:''' The name of the table you want to modify.
*'''values:''' The values that need to be inserted for each field in the table. The default SQL syntax is used, which means that the = is used to set a value, the ' is used to indicate a value and the , is used as a delimiter for multiple values (e.g. ''field1 = 'testA', field2 = 'testB' as values). Field names are optional. If you don't use field names, the table's order of fields is used (e.g. ''testA,testB'' as values).
*'''values:''' The values that need to be inserted for each field in the table. The default SQL syntax is used, which means that the = is used to set a value, the ' is used to indicate a value and the , is used as a delimiter for multiple values (e.g. ''field1 = 'testA', field2 = 'testB' as values). Field names are optional. If you don't use field names, the table's order of fields is used (e.g. ''testA,testB'' as values).


===Returns===
===Returns===
The function returns a ''boolean'' which is ''true'' on success, and ''false'' on failure.
The function returns a ''boolean'' which is ''true'' on success, and ''false'' on failure.
==Example==
<syntaxhighlight lang="lua">
function onMapLoad ()
-- create our table, if it doesn't already exist
executeSQLCreateTable ( "players", "clothes_head_texture TEXT, clothes_head_model TEXT, player TEXT" )
end
function onPlayerSpawn ( spawnpoint, team )
sourcename = getClientName ( source ) -- get the player's name
-- try to retrieve the player data from the db
result = executeSQLSelect ( "players", "player", "player = '" .. sourcename .. "'" )
if ( result == false ) 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][1] )
outputChatBox ( "Your head model is " .. result[1][2] )
end
</syntaxhighlight>


==See Also==
==See Also==
{{Registry_functions}}
{{Registry_functions}}

Revision as of 15:33, 23 September 2006

This function adds a row in the database.

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]INSERT INTO <table> VALUES <values>

Syntax

bool executeSQLInsert ( string table, string values )

Required Arguments

  • table: The name of the table you want to modify.
  • values: The values that need to be inserted for each field in the table. The default SQL syntax is used, which means that the = is used to set a value, the ' is used to indicate a value and the , is used as a delimiter for multiple values (e.g. field1 = 'testA', field2 = 'testB' as values). Field names are optional. If you don't use field names, the table's order of fields is used (e.g. testA,testB as values).

Returns

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


Example

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

function onPlayerSpawn ( spawnpoint, team )	
	sourcename = getClientName ( source )	-- get the player's name
	
	-- try to retrieve the player data from the db
	result = executeSQLSelect ( "players", "player", "player = '" .. sourcename .. "'" )
	if ( result == false ) 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][1] )
	outputChatBox ( "Your head model is " .. result[1][2] )	
end

See Also