Repository navigation
LuaAPI Database
Queries run on the database thread: the GameServer does not freeze while they run.
Important
Always pass text typed by a player through DB.Escape before putting it into SQL (prevents SQL injection).
DB.Query(sql, label, param) → number
Sends a query that returns rows. The result arrives later in onDBQueryResult. Returns 1 if the query was sent, 0 if sql is empty.
| Argument | Meaning |
|---|---|
sql |
The SQL text. |
label |
Your own tag, to know which query the result belongs to. |
param |
Your own string, e.g. tostring(aIndex) or aIndex .. ":" .. name. |
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)DB.Execute(sql) → nothing
Runs an INSERT / UPDATE / DELETE in the background. There is no result and the function returns nothing. An empty sql does nothing.
local sql = string.format("UPDATE my_table SET points = points + 10 WHERE account_id=%d",
Player.GetAccountID(aIndex))
DB.Execute(sql)DB.Escape(text) → string
Returns text with the special characters escaped, ready to be put between quotes ('...') in SQL.
-- typedName: text typed by the player (e.g. a command argument)
local function FindCharacter(aIndex, typedName)
local sql = string.format("SELECT * FROM t WHERE name='%s'", DB.Escape(typedName))
DB.Query(sql, "FIND_NAME", tostring(aIndex))
endonDBQueryResult(label, param, result) → number
Called on the main thread when a DB.Query result is ready. It lives in System\CallbacksDB.lua.
| Argument | Meaning |
|---|---|
label |
The label sent with the query. |
param |
The string sent with the query. |
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).
Modules do not edit CallbacksDB.lua: they attach with BridgeFunctionAttach("OnSQLAsyncResult", "MyFunction"). An attached function returns 1 when it handled the result (the next modules are skipped), 2 to run the query again, anything else to let the next module look at it.
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).
-- 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)
endThe same handler as a module:
function MyPoints_OnResult(label, param, result)
if label ~= "MY_POINTS" then return 0 end
local aIndex = tonumber(string.match(param, "^(%d+):"))
if result ~= 0 and Player.IsOnline(aIndex) then Message.Send(aIndex, 1, "Points: " .. result[1].points) end
return 1
end
BridgeFunctionAttach("OnSQLAsyncResult", "MyPoints_OnResult")Next: Timers