DbQuery: Difference between revisions
No edit summary |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(14 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server function}} | {{Server function}} | ||
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. | 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. | ||
{{Tip|The server command [[Server_Commands#debugdb|debugdb 2]] will output verbose information on each query to a logging file (usually '''logs/db.log''')}} | |||
{{Important Note|It is strongly recommended to use this function asynchronously, as presented in "This example starts a select query and processes the result in an inline callback function with custom arguments:"}} | |||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
handle dbQuery ( [ function callbackFunction, ] element databaseConnection, string query [, var param1 [, var param2 ...]] ) | handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element databaseConnection, string query [, var param1 [, var param2 ...]] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP||[[connection]]:query}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''databaseConnection:''' | *'''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=== | ||
*'''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 | *'''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 the query handle as the first argument. | ||
*'''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 | *'''callbackArguments:''' An optional table containing extra arguments to be sent to the callback function. | ||
String parameters are automatically escaped as required | *'''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=== | ||
Line 24: | Line 23: | ||
==Example== | ==Example== | ||
This example starts an INSERT query | This example starts an INSERT query and frees the result: | ||
<syntaxhighlight lang="lua"> | |||
local qh = dbQuery( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 ) | |||
dbFree( qh ) | |||
</syntaxhighlight> | |||
This example starts a select query and waits for the result: (server freeze included) | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local qh = dbQuery ( connection, " | local qh = dbQuery( connection, "SELECT * FROM table_name" ) | ||
local result = dbPoll( qh, -1 ) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== | |||
{ | This example starts a select query and processes the result in a callback function: | ||
<syntaxhighlight lang="lua"> | |||
function aaa() | |||
dbQuery( myCallback, connection, "SELECT * FROM table_name" ) | |||
end | |||
function myCallback(qh) | |||
local result = dbPoll( qh, 0 ) -- Timeout doesn't matter here because the result will always be ready | |||
end | |||
</syntaxhighlight> | |||
This example starts a select query and processes the result in an inline callback function with custom arguments: | |||
<syntaxhighlight lang="lua"> | |||
dbQuery( function(qh, tag, score) | |||
local result = dbPoll( qh, 0 ) -- Timeout doesn't matter here because the result will always be ready | |||
outputDebugString( tag ) -- Prints "hello" | |||
outputDebugString( score ) -- Prints 2000 | |||
end | |||
,{"hello",2000}, connection, "SELECT * FROM table_name" ) | |||
</syntaxhighlight> | |||
'''''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== | ||
{{ | {{SQL_functions}} |
Latest revision as of 15:34, 7 November 2024
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.
Tip: The server command debugdb 2 will output verbose information on each query to a logging file (usually logs/db.log) |
Important Note: It is strongly recommended to use this function asynchronously, as presented in "This example starts a select query and processes the result in an inline callback function with custom arguments:" |
Syntax
handle dbQuery ( [ function callbackFunction, [ table callbackArguments, ] ] element databaseConnection, string query [, var param1 [, var param2 ...]] )
OOP Syntax Help! I don't understand this!
- Method: connection:query(...)
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
- 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 the query handle as the first argument.
- callbackArguments: An optional table containing extra arguments to be sent to the callback function.
- 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 a query handle unless the connection is incorrect, in which case it return false.
Example
This example starts an INSERT query and frees the result:
local qh = dbQuery( connection, "INSERT INTO table_name VALUES (?,?,?)", "aaa", "bbb", 10 ) dbFree( qh )
This example starts a select query and waits for the result: (server freeze included)
local qh = dbQuery( connection, "SELECT * FROM table_name" ) local result = dbPoll( qh, -1 )
This example starts a select query and processes the result in a callback function:
function aaa() dbQuery( myCallback, connection, "SELECT * FROM table_name" ) end function myCallback(qh) local result = dbPoll( qh, 0 ) -- Timeout doesn't matter here because the result will always be ready end
This example starts a select query and processes the result in an inline callback function with custom arguments:
dbQuery( function(qh, tag, score) local result = dbPoll( qh, 0 ) -- Timeout doesn't matter here because the result will always be ready outputDebugString( tag ) -- Prints "hello" outputDebugString( score ) -- Prints 2000 end ,{"hello",2000}, connection, "SELECT * FROM table_name" )
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