Repository navigation
LuaAPI Database
itvnnnd87 edited this page Oct 24, 2026
·
2 revisions
Sends a query that runs on the database thread (the GameServer does not freeze). The result arrives in onDBQueryResult(label, param, result) in System\CallbacksDB.lua:
| Argument | Meaning |
|---|---|
label |
Your own tag, to know which query the result belongs to. |
param |
Your own string, e.g. tostring(aIndex) or aIndex .. ":" .. name. |
result |
{ [1] = { column = value, ... }, ... }: numeric columns → number, text → string, NULL → nil. No rows or an error → 0. |
Return 1 from onDBQueryResult to run the same query again (at most 20 times).
Warning
The result can arrive a few hundred milliseconds later, when the character may already have left. Check again before using it, e.g. compare the name in param with Player.GetName(aIndex).
-- Send the query
local name = Player.GetName(aIndex)
DB.Query(string.format("SELECT points FROM my_table WHERE account_id=%d", Player.GetAccountID(aIndex)),
"MY_POINTS", aIndex .. ":" .. name)
-- System\CallbacksDB.lua
function onDBQueryResult(label, param, result)
if label == "MY_POINTS" then
local idx, name = string.match(param, "^(%d+):(.*)$")
local aIndex = tonumber(idx)
if not Player.IsOnline(aIndex) or Player.GetName(aIndex) ~= name then return 0 end -- already left
if result == 0 then return 0 end -- no rows
Message.Send(aIndex, 1, "Points: " .. result[1].points)
return 0
end
return BridgeFunction_OnSQLAsyncResult(label, param, result)
endRuns an INSERT / UPDATE / DELETE that needs no result.
Important
Always use it when putting text typed by a player into SQL (prevents SQL injection).
local sql = string.format("SELECT * FROM t WHERE name='%s'", DB.Escape(name))Next: Timers