tw.database
Side: Server
MySQL table and column management via oxmysql. The module loads oxmysql automatically — do not include @oxmysql/lib/MySQL.lua in your fxmanifest or declare oxmysql as a dependency.
Functions
tw.database.table_exists(table_name)
Checks if a table exists in the database.
| Parameter | Type | Description |
|---|---|---|
table_name | string | Table name to check |
Returns: boolean
Alias: tw.database.doesTableExist(table_name)
tw.database.ensure_table(table_name, definition)
Creates a table if it does not already exist.
| Parameter | Type | Description |
|---|---|---|
table_name | string | Table name |
definition | string | SQL column definitions (everything inside the parentheses of CREATE TABLE) |
Returns: boolean — true if the table was created or already existed.
Alias: tw.database.addTable(table_name, definition)
tw.database.ensure_column(table_name, column_name, definition)
Adds a column to an existing table if it does not already exist.
| Parameter | Type | Description |
|---|---|---|
table_name | string | Table name |
column_name | string | Column name to add |
definition | string | SQL column definition (e.g., 'VARCHAR(255) NOT NULL DEFAULT ""') |
Returns: boolean
Alias: tw.database.addColumn(table_name, column_name, definition)
tw.database.ensure_trigger(trigger_name, definition)
Creates a trigger if it does not already exist.
| Parameter | Type | Description |
|---|---|---|
trigger_name | string | Trigger name |
definition | string | Full SQL trigger definition |
Returns: boolean
Alias: tw.database.addTrigger(trigger_name, definition)
Examples
Table and column setup at startup
tw.require("database")
tw.ready(function()
-- Create table
tw.database.ensure_table('twinded_collect', [[
`id` INT NOT NULL AUTO_INCREMENT,
`charidentifier` INT NOT NULL,
`item` VARCHAR(100) NOT NULL,
`amount` INT NOT NULL DEFAULT 0,
`last_collect` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
]])
-- Add a column to an existing table (safe if already present)
tw.database.ensure_column('twinded_collect', 'zone', 'VARCHAR(50) DEFAULT NULL')
end)Direct SQL queries
After tw.database is loaded, you have access to the standard oxmysql functions:
-- Select multiple rows
local rows = MySQL.query.await('SELECT * FROM twinded_collect WHERE charidentifier = ?', { charId })
-- Select a single row
local row = MySQL.single.await('SELECT * FROM twinded_collect WHERE id = ?', { id })
-- Update
MySQL.update.await('UPDATE twinded_collect SET amount = ? WHERE id = ?', { newAmount, id })
-- Insert
MySQL.insert.await('INSERT INTO twinded_collect (charidentifier, item, amount) VALUES (?, ?, ?)', { charId, "apple", 5 })Notes
- Always call
tw.require("database")before using this module. - Do not add
@oxmysql/lib/MySQL.luato your fxmanifest —tw.databasehandles it. - Do not declare
oxmysqlin your script's dependencies —twinded_libsmanages it. - Use
ensure_tableandensure_columnintw.ready()so the database is set up before any queries run.

