Database
The Database module makes it easy to use an SQLite database in your Lua scripts. You can store and load data like player info, settings, or anything else โ and you donโt need to worry about opening or closing the database yourself. Just set it up once and run your queries.
Functionsโ
Initializeโ
Sets the file path for your database. This only needs to be called once.
- path:
stringโ where to store the .db file
Example
Database.Initialize('Saved/Database/lua.db')
Executeโ
Runs a SQL command like CREATE TABLE, INSERT, or UPDATE.
- query:
string - params:
table - returns:
boolean
Example
Database.Execute('CREATE TABLE IF NOT EXISTS PlayerData (PlayerID TEXT PRIMARY KEY, PlayerName TEXT, Score INTEGER)')
ExecuteAsyncโ
Same as Execute, but runs in the background so it wonโt freeze the game.
- query:
string - params:
table - callback:
function(success: boolean)
Example
Database.ExecuteAsync('DELETE FROM PlayerData WHERE PlayerID = ?',{ 'p01' },function(success)
print("Deleted:", success)
end)
Selectโ
Runs a SQL SELECT query and returns a list of rows.
- query:
string - params:
table - returns:
tableโ each row has .Columns
Example
local results = Database.Select('SELECT PlayerName, Score FROM PlayerData WHERE PlayerID = ?',{ 'p01' })
for _, row in ipairs(results) do
print(row.Columns.PlayerName, row.Columns.Score)
end
SelectAsyncโ
Same as Select, but runs in the background
- query:
string - params:
table - callback:
function
Example
Database.SelectAsync('SELECT * FROM PlayerData', {}, function(rows)
for _, row in ipairs(rows) do
print("Player:", row.Columns.PlayerName)
end
end)