Function
dbPoll
Collects the result of a dbQuery: the rows, the number of affected rows and the last inserted ID. If you just want to run a query and get the rows, dbFetch is simpler.
Syntax
table dbPoll(element queryHandle [, int timeout = 0])Arguments
queryHandleelement- A handle from
dbQuery. timeoutint, optional = 00- do not wait (returnsnilwhile there is no result yet),-1- wait until the database answers, a number - wait at most that many milliseconds.
Differences from MTA:SA
- These MTA:SA arguments are not here:
bool multipleResults = false
In MTA:SA
dbPoll(db-query queryHandle, int timeout, bool multipleResults = false)Returns
A table of rows (each row is a column = value table), the affected row count and the inserted ID. The same two numbers are also on the table itself: rows.affectedRows and rows.insertId. nil while the result is not ready yet, false with an error code and message if the query failed.
Example
dbQuery(function(q)
local rows, err, msg = dbPoll(q, 0)
if not rows then
print("Błąd bazy: " .. tostring(msg))
return
end
for _, row in ipairs(rows) do
print(row.serial, row.kasa)
end
end, db, "SELECT serial, kasa FROM gracze ORDER BY kasa DESC LIMIT 10")
Notes
Waiting inside async does not stop the server. Inside async(function() ... end) a dbPoll(q, -1) wait pauses only that task, while the game and other scripts keep running. Outside a task the server stands still until the database answers, and a longer wait prints a warning in the console.
See also
dbFetchRuns a query and returns its result right away. One call instead ofdbQuery,dbPollanddbFree. Works with SELECT, INSERT, UPDATE and DELETE.dbQuerySends a query that returns a result. Collect it withdbPollor in a callback that receives the query handle.asyncRuns a function as a task (coroutine). The code in the task runs immediately until its first wait (sleep,await,waitForEvent). Then it yields and the server and the game keep running. When the wait is over, the task continues from where it stopped. This lets you write steps spread over time (a countdown, a database query, an HTTP response, waiting for a player) one after another, without nested callbacks and timers.