LM database library Back


Core functions

db:execute(query)

Execute given query

db:escape(value)

Escape given value so it can be used in a query

db:query(query, ...)

Execute given query, question marks in the query are replaced by additional parameters


INSERT/UPDATE/DELETE helpers

Note: Lua tables passed in values and where must not have fields that are not present in given database table. Otherwise query will fail

db:insert(tablename, values)

Perform INSERT query based on given values

db:update(tablename, values, where)

Perform UPDATE query based on given values and where parameter

db:delete(tablename, where)

Perform DELETE query based on where parameter


SELECT helpers

Note: parameters must be passed in the same way as for db:query() function

db:getone(query, ...)

Get first field of first matching row from given query

db:getrow(query, ...)

Get first matching row from given query

db:getlist(query, ...)

Get complete query result as Lua table, where each table item is first field from each row

db:getall(query, ...)

Get complete query result as Lua table, where each table item is Lua table with field -> value mapping


Examples

-- Query parameter replacement
db:query('UPDATE table SET field=? WHERE id=?', 'test', 42)
-- INSERT INTO table (id, value) VALUES (42, 'test')
db:insert('table', {
  id = 42,
  value = 'test',
})
-- UPDATE table SET value='test' WHERE id=42
db:update('table', { value = 'test' }, { id = 42 })
-- DELETE FROM table WHERE id=42
db:delete('table', { id = 42 })