Skip to main content

Database

HappinessMP natively supports MySQL X and SQLite.

Server Functions

NameDescription
ConnectConnects to the database server.
CloseCloses the database connection.
ExecuteExecutes a SQL statement with given arguments.
SelectRetrieves data based on a SQL query and arguments.
InsertInserts data into a table and returns the insert ID.

Connect

Connects to the database server.

Database.Connect(int type, string url)

info

The MySQL implementation is based on the X DevAPI. A MySQL server with X plugin is therefore required.

Example:

-- MySQL
Database.Connect(0, "mysqlx://user:password@host:port/database")

-- SQLite
Database.Connect(1, "database.db")

Close

Closes the database connection.

Database.Close()


Execute

This function executes a SQL statement using the provided arguments. It returns the number of rows affected by the execution.

Synchronous (blocking):
int affectedRows = Database.Execute(string statement, list params)

Asynchronous (non-blocking):
Database.ExecuteAsync(string statement, list params, [optional] function callback)

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/register" then
Events.CallRemote("registerPlayer", { Player.GetRockstarID() })
end
end)

-- in server script:
Events.Subscribe("registerPlayer", function(rid)
-- sync:
local affectedRows = Database.Execute("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", { rid, 1000, 0 })

-- async:
Database.ExecuteAsync("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", { rid, 1000, 0 }, function(affectedRows)
Console.Log(affectedRows)
end)
end, true)

Select

This function executes a SQL statement with the given arguments. It returns the resulting data as a table.

Synchronous (blocking):
list result = Database.Select(string statement, list params)

Asynchronous (non-blocking):
Database.SelectAsync(string statement, list params, [optional] function callback)

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/login" then
Events.CallRemote("loginPlayer", { Player.GetRockstarID() })
end
end)

-- in server script:
Events.Subscribe("loginPlayer", function(rid)
-- sync:
local result = Database.Select("SELECT * FROM users WHERE rockstarid = ?", { rid })

if #result > 0 then
Console.Log("Player exist (money: " .. result[1]["money"] .. ", admin: " .. result[1]["admin"] .. ")")
end

-- async:
Database.SelectAsync("SELECT * FROM users WHERE rockstarid = ?", { rid }, function(result)
if #result > 0 then
Console.Log("Player exist (money: " .. result[1]["money"] .. ", admin: " .. result[1]["admin"] .. ")")
end
end)
end, true)

Insert

This function inserts data into a table using the provided SQL statement and arguments. It returns the ID of the newly inserted row.

Synchronous (blocking):
int insertId = Database.Insert(string statement, list params)

Asynchronous (non-blocking):
Database.InsertAsync(string statement, list params, [optional] function callback)

warning

Needs an auto-incremented primary key to work.

Example:

-- in client script:
Events.Subscribe("chatCommand", function(command)
if command == "/register" then
Events.CallRemote("registerPlayer", { Player.GetRockstarID() })
end
end)

-- in server script:
Events.Subscribe("registerPlayer", function(rid)
-- sync:
local insertId = Database.Insert("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", { rid, 1000, 0 })

-- async:
Database.InsertAsync("INSERT INTO users (rockstarid, money, admin) VALUES (?, ?, ?)", { rid, 1000, 0 }, function(insertId)
Console.Log(insertId)
end)
end, true)