DbExec: Difference between revisions
(Created page with "__NOTOC__ {{Server function}} {{New feature|3.0120|1.2| '''Available only in MTA SA 1.1.1 r3341 and onwards''' }} This function executes a database query using the supplied conn...") |
Fernando187 (talk | contribs) No edit summary |
||
(11 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server function}} | {{Server function}} | ||
This function executes a database query using the supplied connection. No query result is returned. | |||
{{Tip|The server command [[Server_Commands#debugdb|debugdb 2]] will output verbose information on each query to a logging file (usually '''logs/db.log''')}} | |||
This function executes a database query using the supplied connection. No result is returned. | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool dbExec ( element databaseConnection, string query [, var param1 [, var param2 ...]] ) | bool dbExec ( element databaseConnection, string query [, var param1 [, var param2 ...]] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[connection]]:exec}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''databaseConnection:''' A database connection element previously returned from [[dbConnect]] | *'''databaseConnection:''' A database connection element previously returned from [[dbConnect]] | ||
*'''query:''' An SQL query. Positions where parameter values will be inserted are marked with a | *'''query:''' An SQL query. Positions where parameter values will be inserted are marked with a '''?''' | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
*'''paramX:''' A variable number of parameters. These must be strings or numbers - it is important to make sure they are of the correct type. Also, the number of parameters passed must be equal to the number of | *'''paramX:''' A variable number of parameters. These must be strings or numbers - it is important to make sure they are of the correct type. Also, the number of parameters passed must be equal to the number of '''?''' characters in the query string. | ||
String parameters are automatically escaped as required | String parameters are automatically quoted and escaped as required. (If you do not want a string quoted, use '''??''') Make sure that numbers are in number format as a string number is treated differently. | ||
===Returns=== | ===Returns=== | ||
Returns ''true'' unless the connection is incorrect, in which case it | Returns ''true'' unless the connection is incorrect, in which case it returns ''false''. | ||
==Example== | ==Example== | ||
Line 28: | Line 25: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | |||
This example shows how to use '''??''' for parts of the query that are not column values: | |||
<syntaxhighlight lang="lua"> | |||
dbExec( connection, "UPDATE ?? SET ??=?", tableName, columnName, columnValue ) | |||
</syntaxhighlight><br> | |||
'''''Note''': It is usually good practice to surround table and column names with backticks (`) in case they contain spaces or SQL keywords (and therefore cause syntax errors). This is especially true when using variables for table and column names, as potential problems may not be apparent when the script is first written.''<br> | |||
This example shows how to use backticks and '''??''' for parts of the query that are not column values: | |||
<syntaxhighlight lang="lua"> | |||
dbExec( connection, "UPDATE `??` SET `??`=?", tableName, columnName, columnValue ) | |||
</syntaxhighlight> | |||
==See Also== | ==See Also== | ||
{{Registry_functions}} | {{Registry_functions}} |
Latest revision as of 15:35, 7 November 2024
This function executes a database query using the supplied connection. No query result is returned.
Tip: The server command debugdb 2 will output verbose information on each query to a logging file (usually logs/db.log) |
Syntax
bool dbExec ( element databaseConnection, string query [, var param1 [, var param2 ...]] )
OOP Syntax Help! I don't understand this!
- Method: connection:exec(...)
Required Arguments
- databaseConnection: A database connection element previously returned from dbConnect
- query: An SQL query. Positions where parameter values will be inserted are marked with a ?
Optional Arguments
- paramX: A variable number of parameters. These must be strings or numbers - it is important to make sure they are of the correct type. Also, the number of parameters passed must be equal to the number of ? characters in the query string.
String parameters are automatically quoted and escaped as required. (If you do not want a string quoted, use ??) Make sure that numbers are in number format as a string number is treated differently.
Returns
Returns true unless the connection is incorrect, in which case it returns false.
Example
This example executes an INSERT query:
dbExec( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 )
This example shows how to use ?? for parts of the query that are not column values:
dbExec( connection, "UPDATE ?? SET ??=?", tableName, columnName, columnValue )
Note: It is usually good practice to surround table and column names with backticks (`) in case they contain spaces or SQL keywords (and therefore cause syntax errors). This is especially true when using variables for table and column names, as potential problems may not be apparent when the script is first written.
This example shows how to use backticks and ?? for parts of the query that are not column values:
dbExec( connection, "UPDATE `??` SET `??`=?", tableName, columnName, columnValue )
See Also