This resource is a standalone fivem wrapper for sql.js.
It provides the user with Lua Exports so you can easily access the sqlite database from your Lua Scripts.
If you like my work and want to help keep open source alive, please consider donating to my ko-fi❤️
Sqlite is very convenient. There is no need to setup a database and establish a connection. Your whole database lies inside one single file which can be easily backed up and transfered
- Download the resource and place it inside your resource folder
- Add
ensure u5_sqlite
to you cfg - Add
local db = exports["u5_sqlite"]
to your server script - You can now use the exports in your server scripts
- I also recommend the vscode extension SQLite3 Editor for viewing and editing the database manually
- tableName | any string
- columns | arrays that consist of columnName, columnsDatatype and constraints in that order
db:createTable("users", { {"id", "INTEGER", "PRIMARY KEY AUTOINCREMENT"}, {"name", "TEXT"}, {"age", "INTEGER"}, })
Results in Query: CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT ,age INTEGER )
- tableName | any string
- columnsAndValues | An object that has the names of the columns you want to insert as its keys, and their correspondng values as its values
db:insert("users", { name = "Adrian", age = 23 })
Results in Query: INSERT INTO users (name,age) VALUES (:name,:age)
And Parameters : {":name": "Adrian", ":age": 23}
- tableName | any string
- columnsAndValues | An object that has the names of the columns you want to update as its keys, and their correspondng values as its values
- where | An object that has the colums as its keys and the values that the corresponding column has to equal as its value. Multiple key:value pairs are chained together with AND | Or a string if rawWhere is true
- rawWhere (optional) | If this is true, the where parameter will be interpreted as a string for complex queries
db:update("users", { name = "John", age = 42 }, { id = 1, name = "Adrian", } )
Results in Query: UPDATE users SET name=:nameVal,age=:ageVal WHERE (id=:idIf AND name=:nameIf)
And Parameters { ":nameVal": "John", ":ageVal": 42, ":idIf":1, ":nameIf": "Adrian"}
db:update("users", { name = "John", age = 42 }, "id=1", true )
Results in Query: UPDATE users SET name=:nameVal,age=:ageVal WHERE (id = 1)
And Parameters {":nameVal":"John", ":ageVal": 43}
- tableName | any string
- where | An object that has the colums as its keys and the values that the corresponding column has to equal as its value. Multiple key:value pairs are chained together with AND | Or a string if rawWhere is true
- rawWhere (optional) | If this is true, the where parameter will be interpreted as a string for complex queries
db:delete("users", { id = 1, name = "John", } )
Results in Query: DELETE FROM users WHERE (id=:id AND name=:name)
And Parameters: {":id":1, ":name": "John"}
db:delete("users", "id=1 AND name = 'john'", true)
Results in Query: DELETE FROM users WHERE (id=1 AND name = 'John')
- query | Any valid sql query. As persql.js to pass parameters, placeholders need the be prefixed with a ":" in the query
- params | The parameter object where the key is the name of the placeholder and the value its value
Can be any string and parameter combination that is allowed in sql.js´s "run" function
- query | Any valid sql query
db:executeRaw("DROP TABLE users")
- tableName | any string
- columns | Array that consists of the column names that you want to retrive
- where | An object that has the colums as its keys and the values that the corresponding column has to equal as its value. Multiple key:value pairs are chained together with AND | Or a string if rawWhere is true
- rawWhere (optional) | If this is true, the where parameter will be interpreted as a string for complex queries
Returns an array of objects. The objects have the columns as their keys and their corresponding values as their values.
db:select("users", {"name","age"}, {id=1})
Results in Query: SELECT name,age FROM users WHERE (id=:id)
And Parameters: {":id":1}
And Returns: [{"age":42,"name":"John"}]