Database
MySQL/MariaDB (or SQLite for tests) through the MySQL table, compatible with oxmysql. Calls never block the server.
MySQL.query
Section titled “MySQL.query”MySQL.query(query, parameters?, cb?)Run a query. SELECT returns the rows; other statements return { affectedRows, insertId }. Server only.
Add .await to wait for the result in the current thread instead of a callback: local rows = MySQL.query.await(sql, params). On an error the callback is not called and the error is logged; .await raises the error in the calling thread.
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL with ? or @name placeholders. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: table) |
Called with the result. Leave it out and use .await to get the result directly. |
MySQL.query('SELECT name, money FROM users WHERE job = ?', { 'police' }, function(rows) for _, row in ipairs(rows) do print(row.name, row.money) endend)
local rows = MySQL.query.await('SELECT * FROM users WHERE identifier = @id', { id = identifier })Compared to FiveM: Same as oxmysql. NULL columns are missing from the row table, TINYINT(1) becomes true/false, DATE/DATETIME/TIMESTAMP become milliseconds since 1970 (UTC).
MySQL.single
Section titled “MySQL.single”MySQL.single(query, parameters?, cb?)Run a query and return only the first row. Server only.
Add .await to wait for the result in the current thread instead of a callback: local row = MySQL.single.await(sql, params). nil if there is no row.
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: table?) |
Called with the result. Leave it out and use .await to get the result directly. |
local user = MySQL.single.await('SELECT * FROM users WHERE identifier = ? LIMIT 1', { identifier })if user then print(user.money) endCompared to FiveM: Same as oxmysql.
MySQL.scalar
Section titled “MySQL.scalar”MySQL.scalar(query, parameters?, cb?)Run a query and return the first column of the first row. Server only.
Add .await to wait for the result in the current thread instead of a callback: local value = MySQL.scalar.await(sql, params).
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: any) |
Called with the result. Leave it out and use .await to get the result directly. |
local count = MySQL.scalar.await('SELECT COUNT(*) FROM vehicles WHERE owner = ?', { identifier })Compared to FiveM: Same as oxmysql.
MySQL.insert
Section titled “MySQL.insert”MySQL.insert(query, parameters?, cb?)Run an INSERT and return the new ID. Server only.
Add .await to wait for the result in the current thread instead of a callback: local id = MySQL.insert.await(sql, params).
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: integer) |
Called with the result. Leave it out and use .await to get the result directly. |
local id = MySQL.insert.await('INSERT INTO bills (owner, amount) VALUES (?, ?)', { identifier, 250 })Compared to FiveM: Same as oxmysql.
MySQL.update
Section titled “MySQL.update”MySQL.update(query, parameters?, cb?)Run an UPDATE or DELETE and return the number of changed rows. Server only.
Add .await to wait for the result in the current thread instead of a callback: local count = MySQL.update.await(sql, params).
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: integer) |
Called with the result. Leave it out and use .await to get the result directly. |
local changed = MySQL.update.await('UPDATE users SET money = money + ? WHERE identifier = ?', { 100, identifier })Compared to FiveM: Same as oxmysql.
MySQL.prepare
Section titled “MySQL.prepare”MySQL.prepare(query, parameters, cb?)Run a statement with ? placeholders, also many times with a list of parameter lists. Server only.
Add .await to wait for the result in the current thread instead of a callback: local result = MySQL.prepare.await(sql, params).
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL with ? placeholders. |
parameters |
any[]|any[][] |
One list of values, or a list of lists to run the statement once per entry. |
cb (optional) |
fun(result: any) |
Called with the result. Leave it out and use .await to get the result directly. |
MySQL.prepare('INSERT INTO items (owner, name) VALUES (?, ?)', { { 1, 'bread' }, { 1, 'water' } })local money = MySQL.prepare.await('SELECT money FROM users WHERE id = ?', { 1 })Compared to FiveM: Same as oxmysql: one row with one column returns the value, one row returns the row, more rows return the list. INSERT returns the new ID, other statements the number of changed rows. Runs as a normal query on the server.
MySQL.rawExecute
Section titled “MySQL.rawExecute”MySQL.rawExecute(query, parameters?, cb?)Run a query and return the raw result. Server only.
Add .await to wait for the result in the current thread instead of a callback: local result = MySQL.rawExecute.await(sql, params).
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: table) |
Called with the result. Leave it out and use .await to get the result directly. |
local rows = MySQL.rawExecute.await('SHOW TABLES')Compared to FiveM: Same as MySQL.query in GAMPEON.
MySQL.execute
Section titled “MySQL.execute”MySQL.execute(query, parameters?, cb?)Old name of MySQL.query. Server only.
| Parameter | Type | Description |
|---|---|---|
query |
string |
SQL. |
parameters (optional) |
table|any[] |
Values for ? placeholders (list) or for @name/:name placeholders (table with keys “name” or “@name”). A list as a value for ? expands for IN (?). |
cb (optional) |
fun(result: table) |
Called with the result. Leave it out and use .await to get the result directly. |
MySQL.execute('DELETE FROM sessions WHERE expires < NOW()')Compared to FiveM: Same as oxmysql.
MySQL.transaction
Section titled “MySQL.transaction”MySQL.transaction(queries, parameters?, cb?)Run several statements in one transaction. If one fails, none of them is kept. Server only.
Add .await to wait for the result in the current thread instead of a callback: local ok = MySQL.transaction.await(queries, params).
| Parameter | Type | Description |
|---|---|---|
queries |
(string|{query: string, values: table?})[] |
Statements as strings (using the shared parameters) or tables with query and values. |
parameters (optional) |
table |
Shared parameters for statements given as strings. |
cb (optional) |
fun(result: boolean) |
Called with the result. Leave it out and use .await to get the result directly. |
local ok = MySQL.transaction.await({ { query = 'UPDATE users SET money = money - ? WHERE id = ?', values = { 100, from } }, { query = 'UPDATE users SET money = money + ? WHERE id = ?', values = { 100, to } },})Compared to FiveM: Same as oxmysql. The callback gets true, or false after a rollback.
MySQL.ready
Section titled “MySQL.ready”MySQL.ready(cb)Run a function once the database is ready. Server only.
| Parameter | Type | Description |
|---|---|---|
cb |
function |
Called in a new thread. |
MySQL.ready(function() print('database ready')end)Compared to FiveM: Same as oxmysql. The server connects before resources start, so it runs right away.
MySQL.Async
Section titled “MySQL.Async”MySQL.Async()Old mysql-async names: fetchAll, fetchScalar, fetchSingle, insert, execute, transaction (callback form). Server only.
MySQL.Async.fetchAll('SELECT * FROM users WHERE identifier = @identifier', { ['@identifier'] = id }, function(rows) end)Compared to FiveM: Same as the mysql-async names that oxmysql still offers. Prefer MySQL.query and friends in new code.
MySQL.Sync
Section titled “MySQL.Sync”MySQL.Sync()Old mysql-async names that wait for the result: fetchAll, fetchScalar, fetchSingle, insert, execute, transaction. Server only.
local rows = MySQL.Sync.fetchAll('SELECT * FROM jobs')Compared to FiveM: Same as the mysql-async names that oxmysql still offers. Only inside a thread.