Skip to content

oxmysql — Reference ​

The database driver. Database name is frontierrp.

Async / Await (preferred in Lua 5.4) ​

lua
-- Query (returns array of rows)
local rows = MySQL.query.await('SELECT * FROM users WHERE identifier = ?', { identifier })

-- Single row
local row = MySQL.single.await('SELECT * FROM users WHERE identifier = ? LIMIT 1', { identifier })

-- Update / Insert / Delete (returns affected rows or insert ID)
local affected = MySQL.update.await('UPDATE users SET money = ? WHERE identifier = ?', { amount, identifier })

-- Insert, returns last insert ID
local id = MySQL.insert.await('INSERT INTO my_table (identifier, data) VALUES (?, ?)', { identifier, json.encode(data) })

-- Scalar value
local count = MySQL.scalar.await('SELECT COUNT(*) FROM users', {})

Callback style (legacy, still valid) ​

lua
MySQL.query('SELECT * FROM users', {}, function(rows)
    for _, row in pairs(rows) do
        print(row.identifier)
    end
end)

Table creation pattern ​

Always create tables on ResourceStart using st.database.addTable():

lua
AddEventHandler('onResourceStart', function(resourceName)
    if GetCurrentResourceName() ~= resourceName then return end

    st.database.addTable('my_resource_data', [[
        id INT AUTO_INCREMENT PRIMARY KEY,
        identifier VARCHAR(64) NOT NULL,
        value INT DEFAULT 0,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        INDEX idx_identifier (identifier)
    ]])
end)