ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Server functions
  3. dbPoll

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 = 0
0 - do not wait (returns nil while 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.

You can read the result more than once. Another dbPoll on the same handle returns the same result, and you do not need dbFree after reading it. A NULL value from the database arrives as false.

See also