DbExec: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
===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 "?" characters in the query string.
*'''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 '''??''')


===Returns===
===Returns===
Line 26: Line 26:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
dbExec( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 )
dbExec( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 )
</syntaxhighlight>
This example shows how to use '''??''' for parts of the query that are not column values:
<syntaxhighlight lang="lua">
dbExec( connection, "SELECT * FROM ?? WHERE ??=?", tableName, columnName, columnValue )
</syntaxhighlight>
</syntaxhighlight>



Revision as of 23:14, 8 November 2011

Available only in MTA SA 1.1.1 r3341 and onwards This function executes a database query using the supplied connection. No result is returned.

Syntax

bool dbExec ( element databaseConnection, string query [, var param1 [, var param2 ...]] )

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 ??)

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, "SELECT * FROM ?? WHERE ??=?", tableName, columnName, columnValue )

Requirements

Minimum server version 1.1.1-9.03341
Minimum client version n/a

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.1.1-9.03341" />

See Also