Both the Legacy mod and the ET:Legacy engine are built-in with SQLite3 database support. The engine allows to execute SQL statement directly in console, while the Legacy mod has access through LuaSQL.


See the Database sample code for an example of basic database usage.

Error handling

LuaSQL is just an abstraction layer that communicates between Lua and a database system. Therefore errors can occur on both levels, that is, inside the database client or inside LuaSQL driver.

Errors such as malformed SQL statements, unknown table names etc. are called database errors and will be reported by the function/method returning nil followed by the error message provided by the database system. Errors such as wrong parameters, absent connection, invalid objects etc., called API errors, are usually program errors and so will raise a Lua error.

This behavior will be followed by all functions/methods described in this document unless otherwise stated.


A LuaSQL driver allows the use of the LuaSQL API with a database management system that corresponds to the driver. To use a driver you have to load it. The example below:

local driver = require "luasql.sqlite3"

loads the SQLite3 driver and returns a table with an entry with the name of the driver (sqlite3 in this case).

Environment objects

An environment object is created by calling the driver’s initialization function that is stored in the table returned when it was loaded, indexed with the same name as the driver (odbc, postgres etc). The following example, will try to create an environment object using the SQLite3 driver:

local driver = require "luasql.sqlite3"
local env = driver.sqlite3()


Closes the environment env. Only successful if all connections pertaining to it were closed first.

Returns true in case of success; false when the object is already closed.


Connects to a data source specified in sourcename using username and password if they are supplied. The sourcename may vary according to each driver. SQLite3 uses a simple database name:

-- get database path
local dbpath = string.gsub(et.trap_Cvar_Get("fs_homepath"), "\\", "/").."/""fs_game").."/"
-- connection
conn = assert(env:connect(dbpath .. "etl.db"))

Returns a connection object.

If desired, Lua scripts can also connect to the engine memory database by the following Lua command:

conn = assert(env:connect("file::memory:?cache=shared"))


The database is only active when the db_mode cvar is set.

To save this in memory database to disk use the saveDB console command. See also the db_url cvar to specify the database path.

Connection objects

A connection object contains specific attributes and parameters of a single data source connection. A connection object is created by calling the environment:connect method.


Closes the connection conn. Only successful if all cursors pertaining to it have been closed and the connection is still open.

Returns true in case of success and false in case of failure.


Commits the current transaction.

Returns true in case of success and false when the operation could not be performed or when it is not implemented.


Executes the given SQL statement.

Returns a cursor object if there are results, or the number of rows affected by the command otherwise.


Rolls back the current transaction.

Returns true in case of success and false when the operation could not be performed or when it is not implemented.


Turns on or off the “auto commit” mode.

Returns true in case of success and false when the operation could not be performed or when it is not implemented.

Cursor objects

A cursor object contains methods to retrieve data resulting from an executed statement. A cursor object is created by using the connection:execute function.


Closes this cursor.

Returns true in case of success and false when the object is already closed.


Retrieves the next row of results.

If fetch is called without parameters, the results will be returned directly to the caller. If fetch is called with a table, the results will be copied into the table and the changed table will be returned. In this case, an optional modestring parameter can be used. It is just a string indicating how the resulting table should be constructed.

The mode string can contain:

  • n: the resulting table will have numerical indices (default)
  • a: the resulting table will have alphanumerical indices

The numerical indices are the positions of the fields in the SELECT statement; the alphanumerical indices are the names of the fields. The optional table parameter is a table that should be used to store the next row. This allows the use of a unique table for many fetches, which can improve the overall performance.

A call to fetch after the last row has already being returned will close the corresponding cursor. There is no guarantee about the types of the results: they may or may not be converted to adequate Lua types by the driver.

Returns data, as above, or nil if there are no more rows.


Returns a list (table) of column names.


Returns a list (table) of column types.

SQLite3 extensions

Besides the basic functionality provided by all drivers, the SQLite3 driver also offers this extra feature:


In the SQLite3 driver, this method adds an optional parameter that indicate the amount of milliseconds to wait for a write lock if one cannot be obtained immediately. See also environment objects.

Returns a connection object.


Escape especial characters in the given string according to the connection’s character set. See also the official documentation of function sqlite3_mprintf.

Returns the escaped string.