Below is a small sample code displaying the basic use of the library.
@@ -117,11 +115,18 @@ for id, name, address in rows (con, "select * from contacts") do
end
-
LuaSQL is a simple interface from Lua to a DBMS. It enables a Lua program to:
+
Connect to ODBC, ADO, Oracle, MySQL, SQLite, JDBC, and PostgreSQL databases;
Execute arbitrary SQL statements;
@@ -75,7 +76,7 @@ as Lua 5.0.
Current Version
LuaSQL version 2.0.0 (for Lua 5.0) is now available for
-download.
+download.
The PostgreSQL driver has been tested on Linux and MacOS X and is compatible
@@ -123,11 +124,11 @@ for Lua 5.1
Version 2.0 has some design modifications and implementation improvements
-
New fetch method: more eficient and more flexible
-
New setautocommit method
-
Lua 5.0 and 5.1 compatible
-
Dynamically loadable or statically linked
-
New drivers for Oracle and MySQL databases
+
New fetch method: more eficient and more flexible
+
New setautocommit method
+
Lua 5.0 and 5.1 compatible
+
Dynamically loadable or statically linked
+
New drivers for Oracle and MySQL databases
Installation
@@ -157,9 +158,9 @@ in the LUA_PATH. If you are using Lua 5.1, nothing should be done.
In order to use LuaSQL over JDBC, make sure that:
LuaSQL jar is in the Java's virtual machine classpath
+
JDBC driver of the desired database is also in the virtual machine classpath
@@ -178,6 +179,7 @@ The implementation is compatible with Lua 5.0 and was coded by
Tomás Guisasola, Eduardo Quintão, Thiago Ponte, Fabio Mascarenhas,
with many invaluable contributions by Michael Roth, Tiago Dionisio,
Leonardo Godinho and Danilo Tuler.
+
LuaSQL 1.0
@@ -186,10 +188,11 @@ Ierusalimschy.
The first implementation was compatible with Lua 4.0a.
Many modifications were made but not distributed by Diego Nehab (ODBC),
Carlos Cassino, Tomás Guisasola and Eduardo Quintão (PostgreSQL).
+
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -72,10 +72,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -95,6 +95,7 @@ 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 like mal-formed SQL statement, unknown table name etc.
are called database errors and
@@ -113,6 +114,7 @@ described in this document unless otherwise stated.
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,
+
env:close()
Closes the environment env.
-Only successful if all connections pertaining to it were closed first.
+Only successful if all connections pertaining to it were closed first.
Returns: true in case of success; false when
the object is already closed.
-
env:connect(sourcename[,username[,password]])
+
env:connect(sourcename[,username[,password]])
Connects to a data source specified in sourcename using
-username and password if they are supplied.
+username and password if they are supplied.
The sourcename may vary according to each driver.
Some use a simple database name, like PostgreSQL, MySQL and SQLite;
the ODBC driver expects the name of the DSN;
the Oracle driver expects the service name;
-the JDBC driver expects a string like "jdbc:<database system>://<database name>", which is specific for each driver.
+the JDBC driver expects a string like "jdbc:<database system>://<database name>", which is specific for each driver.
See also: PostgreSQL,
- and MySQL extensions.
+ and MySQL extensions.
Returns: a connection object.
@@ -163,42 +165,42 @@ method.
-
conn:close()
+
conn:close()
Closes the connection conn.
-Only successful if all cursors pertaining to it were closed first.
+Only successful if all cursors pertaining to it were closed first.
Returns: true in case of success and false when
the object is already closed.
-
conn:commit()
+
conn:commit()
Commits the current transaction.
This feature might not work on database systems that do not implement
-transactions.
+transactions.
Returns: true in case of success and false when
the operation could not be performed or when it is not implemented.
-
conn:execute(statement)
-Executes the given SQL statement.
+
conn:execute(statement)
+Executes the given SQL statement.
Returns: a cursor object
if there are results, or the number of rows affected by the command otherwise.
-
conn:rollback()
+
conn:rollback()
Rolls back the current transaction.
This feature might not work on database systems that do not implement
-transactions.
+transactions.
Returns: true in case of success and false when
the operation could not be performed or when it is not implemented.
-
conn:setautocommit(boolean)
+
conn:setautocommit(boolean)
Turns on or off the "auto commit" mode.
This feature might not work on database systems that do not implement
transactions.
On database systems that do not have the concept of "auto commit mode",
but do implement transactions, this mechanism is implemented by the driver.
-
+
Returns: true in case of success and false when
the operation could not be performed or when it is not implemented.
cur:close()
+Closes this cursor.
Returns: true in case of success and false when
the object is already closed.
-
cur:fetch([table[,modestring]])
-Retrieves the next row of results.
+
cur:fetch([table[,modestring]])
+Retrieves the next row of results.
If fetch is called without parameters,
the results will be returned to the caller directly.
If fetch is called with a table, the results will be copied
@@ -234,29 +236,29 @@ In this case, an optional mode parameter can be used.
It is just a string indicating how the result table should be made.
The mode string can contain:
-
"n" the resulting table will have numerical indices (default)
-
"a" the resulting table will have alphanumerical indices
+
"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 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.
+can improve the overall performance.
There is no guarantee about the types of the results, they can be converted to adequate Lua types by the driver or not.
In the current implementation,
the PostgreSQL and MySQL drivers returns all values as strings
-while the ODBC and Oracle drivers converts them to Lua types.
+while the ODBC and Oracle drivers converts them to Lua types.
Returns: data, as above, or nil if there are no more rows.
Note that this method could return nil as a valid result.
-
cur:getcolnames()
+
cur:getcolnames()
Returns: a list (table) of column names.
-
cur:getcoltypes()
+
cur:getcoltypes()
Returns: a list (table) of column types.
@@ -268,18 +270,18 @@ Returns: a list (table) of column types.
the Postgres driver also offers these extra features:
env:connect(sourcename[,username[,password[,hostname[,port]]]])
In the PostgreSQL driver, this method has two other optional parameters
that indicate the hostname and port to connect.
Also, the first parameter can contain all connection information,
as stated in the documentation for PQconnectdb function
in the PostgreSQL manual
- (e.g. environment:connect("dbname=<name> user=<username>"))
- See also: environment objects
+ (e.g. environment:connect("dbname=<name> user=<username>"))
+ See also: environment objects
Returns: a connection object
env:connect(sourcename[,username[,password[,hostname[,port]]]])
In the MySQL driver, this method has two other optional parameters
that indicate the hostname and port to connect.
- See also: environment objects
+ See also: environment objects
Returns: a connection object
@@ -311,19 +313,19 @@ methods commit, rollback and
the Oracle driver also offers this extra feature: