DbQuery: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Server function}} {{New feature|3.0120|1.2| '''Available only in MTA SA 1.1.1 r3328 and onwards''' }} This function starts a database query using the supplied connec...")
 
No edit summary
Line 8: Line 8:
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
handle dbQuery ( element databaseConnection, string query [, var param1 [, var param2 ...]] )
handle dbQuery ( [ function callbackFunction, ] element databaseConnection, string query [, var param1 [, var param2 ...]] )
</syntaxhighlight>
</syntaxhighlight>


Line 16: Line 16:


===Optional Arguments===
===Optional Arguments===
*'''callbackFunction:''' An optional function to be called when a result is ready. The function will only be called if the result has not already been read with [[dbPoll]]. The function is called with one argument which is the query handle. (Notice: Do not use a 'local' function, it must be global!)
*'''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 escaped as required

Revision as of 18:35, 27 October 2011

Available only in MTA SA 1.1.1 r3328 and onwards This function starts a database query using the supplied connection. Use the returned query handle with dbPoll to get the result, or dbFree if you don't want the result.

Syntax

handle dbQuery ( [ function callbackFunction, ] element databaseConnection, string query [, var param1 [, var param2 ...]] )

Required Arguments

  • databaseConnection: An database connection element previously returned from dbConnect
  • query: An SQL query. Positions where parameter values will be inserted are marked with a "?".

Optional Arguments

  • callbackFunction: An optional function to be called when a result is ready. The function will only be called if the result has not already been read with dbPoll. The function is called with one argument which is the query handle. (Notice: Do not use a 'local' function, it must be global!)
  • 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

Returns

Returns a query handle unless the connection is incorrect, in which case it return false.

Example

This example starts an INSERT query

local qh = dbQuery ( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 )

Requirements

This template will be deleted.

See Also