M MY Multiplayer: Wiki
Side
Kind

dbQuery

Server

Sends an SQL query whose answer is read later with dbPoll.

any dbQuery(element connection, string sql, any ... [, function callback = nil])

Parameters

NameTypeDescription
callbackfunctionIf given, it is called with the query handle when the answer arrives; call dbPoll(query, 0) inside it.default nil
connectionelementA connection from dbConnect.
sqlstringThe SQL statement, with ? and ?? as placeholders.
...anyThe placeholders' values: strings, numbers, booleans or nil.

Returns

A query handle for dbPoll and dbFree. Without callback, inside a coroutine, it waits for the answer and returns the same as dbPoll.

Placeholders and handle lifetime

  • ? takes the next value, escaped: nil becomes NULL, booleans become 1 or 0 and strings are quoted.
  • ?? takes a string as an escaped table or column identifier.
  • The number of values must match the number of placeholders.
  • A handle without callback that nothing references any more is freed automatically by the garbage collector.

Example

dbQuery(function(query)
    local rows = dbPoll(query, 0)
    if rows then
        print(#rows .. " players saved")
    end
end, db, "SELECT * FROM players WHERE money > ?", 100)

See also