The Lua language
LuaSQL Reference Manual
Database connectivity for the Lua language

home · environment · connection · cursor · extensions · example


Introduction

LuaSQL is a simple interface from Lua to a DBMS. It has a collection of drivers to some popular databases (actually PostgreSQL, ODBC, MySQL and Oracle; ADO will be ready soon). LuaSQL defines a simple object-oriented API. All drivers should implement this common API, but each one is free to offer extensions.

LuaSQL defines one single global variable, a table called luasql. This table is used to store the initialization methods of the loaded drivers. These methods are used to create an environment object which is used to create a connection object. A connection object can execute SQL statements and eventually create a cursor object which is used to retrieve data.

Each LuaSQL driver is composed by two files: a C source file that implements the driver functions; and a Lua script used to load the dynamic library. The host application can be statically linked with one or more drivers or they can be loaded dynamically by the corresponding Lua script (this requires the built-in loadlib function Lua 5 offers).

LuaSQL is free software.

Environment objects

An environment object is created by calling the driver's initialization function that is stored into the table luasql with the same name of the driver (odbc, postgres etc.). For example, luasql.odbc(), will try to create an environment object using the ODBC driver.

Methods

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.

Methods

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. See also PostgreSQL and Oracle extensions.

Methods

PostgreSQL extensions

Besides the basic functionality provided by all drivers (see manual), the Postgres driver also offers these extra features:

MySQL extensions

Besides the basic functionality provided by all drivers (see manual), the MySQL driver also offers these extra features: Note: This driver is compatible to version 4.1 of MySQL API, with support to BDB and INNODB tables. MySQL has types of tables that don't support transactions, so with these types, the methods setautocommit, commit and rollback don't work.

Oracle extensions

Besides the basic functionality provided by all drivers (see manual), the Oracle driver also offers this extra feature:

Example

Below is a small sample code displaying the basic use of the library.
-- load driver
require"postgres"
-- create environment object
env = assert (luasql.postgres())
-- connect to data source
con = assert (env:connect("luasql-test"))
-- reset our table
res = con:execute"DROP TABLE people"
res = assert (con:execute[[
  CREATE TABLE people(
    name  varchar(50),
    email varchar(50)
  )
]])
-- add a few elements
list = {
  { name="Jose das Couves", email="jose@couves.com", },
  { name="Manoel Joaquim", email="manoel.joaquim@cafundo.com", },
  { name="Maria das Dores", email="maria@dores.com", },
}
for i, p in pairs (list) do
  res = assert (con:execute(string.format([[
    INSERT INTO people
    VALUES ('%s', '%s')]], p.name, p.email)
  ))
end
-- retrieve a cursor
cur = assert (con:execute"SELECT name, email from people")
-- print all rows
row = cur:fetch ({}, "a")	-- the rows will be indexed by field names
while row do
  print(string.format("Name: %s, E-mail: %s", row.name, row.email))
  row = cur:fetch (row, "a")	-- reusing the table of results
end
-- close everything
cur:close()
con:close()
env:close()
And the output of this script should be:
Name: Jose das Couves, E-mail: jose@couves.com
Name: Manoel Joaquim, E-mail: manoel.joaquim@cafundo.com
Name: Maria das Dores, E-mail: maria@dores.com

Contents

home · environment · connection · cursor · extensions · example


Last modified on Fri Sep 12 18:31:45 BRT 2003