ExecuteSQLCreateTable: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server function}}
{{Server function}}
{{Deprecated|executeSQLQuery|See the examples at executeSQLQuery for equivalent CREATE TABLE usage.}}
This function creates a table in the database. It doesn't do anything when the table already exists. You can use this function in the loading or initialisation step of your script to ensure all the necessary tables that you use actually exist.
This function creates a table in the database. It doesn't do anything when the table already exists. You can use this function in the loading or initialisation step of your script to ensure all the necessary tables that you use actually exist.


The executed SQL query is the following:
The executed SQL query is the following:


<syntaxhighlight lang="lua">[sql]CREATE TABLE IF NOT EXISTS <table> ( <definition> )</syntaxhighlight>
<syntaxhighlight lang="lua">CREATE TABLE IF NOT EXISTS tableName ( definition )</syntaxhighlight>


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool executeSQLCreateTable ( string table, string definition )
bool executeSQLCreateTable ( string tableName, string definition )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''table:''' The name of the table you want to create.
*'''tableName:''' The name of the table you want to create.
*'''definition:''' The definition of the table, this includes the column definitions and constraints in SQL syntax. For each column definition, you start with the name (without any spaces), optionally followed by the [http://www.sqlite.org/datatype3.html SQL data type] and constraint (all separated by spaces). Each column definition is separated by a comma (,) (e.g. ''field1,field2,field3'' or ''field1 INTEGER,field2 TEXT'' as definition). Please refer to the [http://www.sqlite.org/lang_createtable.html SQLite SQL documentation] for more information on creating even more complex tables.
*'''definition:''' The definition of the table, this includes the column definitions and constraints in SQL syntax. For each column definition, you start with the name (without any spaces), optionally followed by the [http://www.sqlite.org/datatype3.html SQL data type] and constraint (all separated by spaces). Each column definition is separated by a comma (,) (e.g. ''field1,field2,field3'' or ''field1 INTEGER,field2 TEXT'' as definition). Please refer to the [http://www.sqlite.org/lang_createtable.html SQLite SQL documentation] for more information on creating even more complex tables.


===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.
 


==Example==
==Example==
This example creates a SQL table when the resource starts.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onMapLoad ()
function createSQLOnStart ()
-- create our table, if it doesn't already exist
-- create our table, if it doesn't already exist
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 ( "onResourceStart", getResourceRootElement(),createSQLOnStart )


</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 15:13, 4 October 2016

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 CREATE TABLE usage.


This function creates a table in the database. It doesn't do anything when the table already exists. You can use this function in the loading or initialisation step of your script to ensure all the necessary tables that you use actually exist.

The executed SQL query is the following:

CREATE TABLE IF NOT EXISTS tableName ( definition )

Syntax

bool executeSQLCreateTable ( string tableName, string definition )

Required Arguments

  • tableName: The name of the table you want to create.
  • definition: The definition of the table, this includes the column definitions and constraints in SQL syntax. For each column definition, you start with the name (without any spaces), optionally followed by the SQL data type and constraint (all separated by spaces). Each column definition is separated by a comma (,) (e.g. field1,field2,field3 or field1 INTEGER,field2 TEXT as definition). Please refer to the SQLite SQL documentation for more information on creating even more complex tables.

Returns

The function returns true on success, and false on failure.

Example

This example creates a SQL table when the resource starts.

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

See Also