Skip to Content
ScriptingDatabase

Database

FiveM resources commonly use MySQL/MariaDB for data persistence.

oxmysql

Modern MySQL library for FiveM.

Installation

Add to fxmanifest.lua:

dependency 'oxmysql'

Usage

-- Query MySQL.query('SELECT * FROM users WHERE id = ?', {userId}, function(result) if result[1] then print('User found:', result[1].name) end end) -- Insert MySQL.insert('INSERT INTO users (name) VALUES (?)', {name}, function(insertId) print('Inserted ID:', insertId) end) -- Update MySQL.update('UPDATE users SET name = ? WHERE id = ?', {newName, userId})

ghmattimysql

Alternative MySQL library.

Usage

MySQL.Async.execute('UPDATE users SET name = @name WHERE id = @id', { ['@name'] = newName, ['@id'] = userId })

Schema Patterns

User Table

CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, identifier VARCHAR(50) UNIQUE, name VARCHAR(100), money INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );

Migrations

Create migration files:

-- migrations/001_add_email.sql ALTER TABLE users ADD COLUMN email VARCHAR(255);

Best Practices

  • Use prepared statements
  • Index frequently queried columns
  • Handle errors gracefully
  • Use transactions for complex operations
Last updated on