ExecuteSQLUpdate: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 11: Line 11:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool executeSQLUpdate ( string table, string set, [ string conditions ] )
bool executeSQLUpdate ( string tableName, string set, [ string conditions ] )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''table:''' The name of the table you want to modify.
*'''tableName:''' The name of the table you want to modify.
*'''set:''' The query you want to execute on that table. The default SQL syntax is used, which means that the = is used to set a value and the ' is used to specify a value (e.g. ''test = 'hi' ''). The , is used as a delimiter for multiple queries.
*'''set:''' The query you want to execute on that table. The default SQL syntax is used, which means that the = is used to set a value and the ' is used to specify a value (e.g. ''test = 'hi' ''). The , is used as a delimiter for multiple queries.


Line 22: Line 22:


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




Line 31: Line 31:
executeSQLCreateTable ( "players", "clothes_head_texture TEXT, clothes_head_model TEXT, player TEXT" )
executeSQLCreateTable ( "players", "clothes_head_texture TEXT, clothes_head_model TEXT, player TEXT" )
end
end
addEventHandler ( "onGamemodeMapStart", getRootElement(), onMapLoad )


function onPlayerSpawn ( spawnpoint, team )
function onPlayerSpawn ( spawnpoint, team )
Line 47: Line 48:
-- get the clothes data for the player
-- get the clothes data for the player
    result = executeSQLSelect ( "players", "clothes_head_texture, clothes_head_model", "player = '" .. sourcename .. "'" )
result = executeSQLSelect ( "players", "clothes_head_texture, clothes_head_model", "player = '" .. sourcename .. "'" )
outputChatBox ( "Your head texture is " .. result[1][1] )
outputChatBox ( "Your head texture is " .. result[1][1] )
outputChatBox ( "Your head model is " .. result[1][2] )
outputChatBox ( "Your head model is " .. result[1][2] )
end
end
addEventHandler ( "onPlayerSpawn", getRootElement(), onPlayerSpawn )
</syntaxhighlight>
</syntaxhighlight>


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

Revision as of 12:33, 21 August 2007

This function updates one or more rows in the database, by using the set parameter and conditions to change field values in specific rows.

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]UPDATE <table> SET <set> WHERE <conditions>

Syntax

bool executeSQLUpdate ( string tableName, string set, [ string conditions ] )

Required Arguments

  • tableName: The name of the table you want to modify.
  • set: The query you want to execute on that table. The default SQL syntax is used, which means that the = is used to set a value and the ' is used to specify a value (e.g. test = 'hi' ). The , is used as a delimiter for multiple queries.

Optional Arguments

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

Returns

The function returns 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
addEventHandler ( "onGamemodeMapStart", getRootElement(), onMapLoad )

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
addEventHandler ( "onPlayerSpawn", getRootElement(), onPlayerSpawn )

See Also