Reference/Native_modules/lu_sqlite_SQLite_databases_original_procedural_module

LoadModule("lu_sqlite") adds the SQLite functions of the original Liberty Unleashed server, kept for old scripts. A database is a handle you pass to every call, and a query returns a result handle that points at its first row. For new scripts use lu_sqlite2 (C.4), which has parameters, error messages and 64 bit integers.

Functions

sqlite_open

sqlite_open(path)

Opens (or creates) a database file, relative to the server folder.

Returns: database handle, or null when the file cannot be opened

sqlite_query

sqlite_query(db, sql)

Runs ONE SQL statement. Every statement returns a result handle, also CREATE / INSERT / UPDATE, and each one must be released with sqlite_free. The result starts on the first row.

Returns: result handle, or null when the SQL is invalid (there is no error message)

sqlite_column_count

sqlite_column_count(result)

The number of columns of the result (0 for statements that return no rows).

Returns: integer

sqlite_column_data

sqlite_column_data(result, column)

The value of a column (0 based) in the current row: integer, float, string or null. Null too when there are no rows, for a column past the end and for BLOB columns. Integers are 32 bit: larger values are cut.

Returns: integer / float / string / null

sqlite_next_row

sqlite_next_row(result)

Moves to the next row: true when there is one, false after the last row. Do not call it again after it returned false.

Returns: bool

sqlite_free

sqlite_free(result)

Releases a result handle. Forgetting it keeps the database busy (and sqlite_close then fails).

Returns: bool (true)

sqlite_close

sqlite_close(db)

Closes the database. Returns false (and stays open) while results of it have not been freed.

Returns: bool

LoadModule("lu_sqlite");
DB <- sqlite_open("Data/server.db");
sqlite_free(sqlite_query(DB, "CREATE TABLE IF NOT EXISTS bans (name TEXT, reason TEXT)"));

// escape quotes yourself: there are no parameters in this module
function Q(s)
{
    local o = "";
    foreach (c in s) o += (c == 39) ? "''" : c.tochar();     // 39 = '
    return "'" + o + "'";
}

function AddBan(name, reason)
{
    local sql = "INSERT INTO bans VALUES (" + Q(name) + ", " + Q(reason) + ")";
    sqlite_free(sqlite_query(DB, sql));
}

function ListBans()
{
    local q = sqlite_query(DB, "SELECT name, reason FROM bans");
    if (q == null) return;
    if (sqlite_column_data(q, 0) != null)          // null = no rows
    {
        do {
            print(sqlite_column_data(q, 0) + ": " + sqlite_column_data(q, 1));
        } while (sqlite_next_row(q));
    }
    sqlite_free(q);
}
NoteNever put player text into the SQL without escaping single quotes as in Q above, or a name like x'); DROP TABLE bans; -- breaks your database. This module comes from the original Liberty Unleashed server; there is no source code for it.
Categories: Native modules