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 thread —
self: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
| Type | Name | Description |
|---|---|---|
int | limit | Limits the maximum number of rows to retrieve When set to 0, no upper limit is applied and all matching rows are returned |
Returns
| Type | Name | Description |
|---|---|---|
promise | promise | Refer Settlements section |
Settlements
| Type | Name | Description |
|---|---|---|
bool | status | true on success, false on failure |
table | resolved | Array of matching rows, where each row is a table of column name to value pairs |
string | rejected | Error message describing the failure |
Examples
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()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()