- Documentation
- Server functions
- dbConnect
Function
dbConnect
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 fromdatabases.jsonor"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
dbQuerySends a query that returns a result. Collect it withdbPollor in a callback that receives the query handle.dbExecRuns a query that returns no result (INSERT, UPDATE, CREATE). Arguments replace the?placeholders.dbPollCollects the result of adbQuery: the rows, the number of affected rows and the last inserted ID. If you just want to run a query and get the rows,dbFetchis simpler.