ServersYour serverFor creatorsForumFAQDocumentationSecurity
Download the launcher
  1. Documentation
  2. Server functions
  3. dbConnect

Function

dbConnect

ServerDatabaseIdentical to MTA:SA

Opens a connection to an SQLite database (a file in the resource folder) or MySQL.

Syntax

element dbConnect(string dbType, string target [, string username = nil, string password = nil, string/table options = nil])

Arguments

dbTypestring
"sqlite" or "mysql".
targetstring
SQLite: a file path in the resource or ":memory:". MySQL: a connection name from databases.json or "dbname=...;host=...;port=...".
usernamestring, optional = nil
MySQL user. Leave out for a named connection.
passwordstring, optional = nil
MySQL password.
optionsstring/table, optional = nil
Extra MySQL options.

Returns

A connection handle or false.

Example

local db = dbConnect("sqlite", "gracze.db")
dbExec(db, "CREATE TABLE IF NOT EXISTS gracze (serial TEXT PRIMARY KEY, kasa INTEGER)")

local mysql = dbConnect("mysql", "glowna")

Notes

Safe database passwords. A named connection (dbConnect("mysql", "glowna")) reads its details from the server's databases.json, which can be encrypted. The password never appears in the resource code, and only resources listed in resources may use it.

The SQLite path stays inside the resource. The database file always lives in the resource folder; paths that leave it are rejected.

See also