self:fetch

Server

Retrieves all rows matching the prepared query from the table


Syntax

local promise = self:fetch(
    limit = 0
)

This function returns a promise and starts processing the query. Use thread:await inside a thread to retrieve the result.

  • Inside a threadself:await(db_query:fetch(...)) yields until the results are retrieved, then returns the resolved values
  • Outside a thread — the promise can still be created, but must be awaited from within a thread to retrieve the result

Parameters

TypeNameDescription
intlimitLimits the maximum number of rows to retrieve
When set to 0, no upper limit is applied and all matching rows are returned

Returns

TypeNameDescription
promisepromiseRefer Settlements section

Settlements

TypeNameDescription
boolstatustrue on success, false on failure
tableresolvedArray of matching rows, where each row is a table of column name to value pairs
stringrejectedError message describing the failure

Examples

Fetch all players with a score greater than 100
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, rows = self:await(db:table("players")
        :select("id", "name", "score")
        :where("score", ">", 100)
        :fetch())

    if ok then
        for i = 1, #rows do
            engine.print("info", rows[i].id, rows[i].name, rows[i].score)
        end
    end
end):resume()
Fetch only the top 5 players
local db = database.create("127.0.0.1", "root", "", "vital_sandbox")

thread.create(function(self)
    local ok, rows = self:await(db:table("players")
        :select("id", "name", "score")
        :where("score", ">", 100)
        :fetch(5))

    if ok then
        for i = 1, #rows do
            engine.print("info", rows[i].id, rows[i].name, rows[i].score)
        end
    end
end):resume()

On this page