lua-mysql
is a MySQL client library for Lua. It is compatible with Lua 5.2.3(or above) and based on the MySQL C API provided by the official MySQL library.
First you need to use mysql = require('luamysql')
to import a table named mysql
(or any other valid name).
-
client, errmsg = mysql.newclient(dbarg)
- Attempts to establish a connection to a MySQL server specified by
dbarg
. If successfully executed, a valid MySQL clientclient
, plus anil
forerrmsg
, are returned; otherwiseclient
will benil
and an error messageerrmsg
is returned. Seeluamysql-demo.lua
for more details aboutdbarg
.
- Attempts to establish a connection to a MySQL server specified by
-
errmsg = client:ping()
- Checks whether the connection to the server is working. If the connection has gone down and auto-reconnect is enabled an attempt to reconnect is made. If the connection is down and auto-reconnect is disabled, an error message
errmsg
is returned.
- Checks whether the connection to the server is working. If the connection has gone down and auto-reconnect is enabled an attempt to reconnect is made. If the connection is down and auto-reconnect is disabled, an error message
-
errmsg = client:selectdb(dbname)
- Causes the database specified by
dbname
to become the default (current) database of theclient
.nil
is returned if successfully executed, otherwise an error messageerrmsg
is returned.
- Causes the database specified by
-
errmsg = client:setcharset(charset)
- Sets the default character set to be
charset
for the current connection.nil
is returned if successfully executed, otherwise an error messageerrmsg
is returned.
- Sets the default character set to be
-
result, errmsg = client:escape(str)
- Converts the
str
to a legal SQL string that can be used in a SQL statement. If successfully executed, aresult
containing a valid string, and anil
forerrmsg
, are returned; otherwise theresult
will benil
, and an error messageerrmsg
is returned.
- Converts the
-
result, errmsg = client:execute(sqlstr)
- Executes a SQL statement
sqlstr
. If successfully executed, aresult
containing all information, and anil
forerrmsg
, are returned; otherwise theresult
will benil
, and the error messageerrmsg
tells what happened.
- Executes a SQL statement
-
size = result:size()
- Returns the number of record(s) in the
result
.nil
is returned if error occurs.
- Returns the number of record(s) in the
-
fieldnamelist = result:fieldnamelist()
- Returns the fieldname list for the
result
.nil
is returned if error occurs.
- Returns the fieldname list for the
-
result:recordlist()
- Returns an iteration closure function which can be used in the
for ... in
form.nil
is returned if error occurs. Each record returned by the iterator function is an array containing values corresponding to the fieldname list which is the result ofresult:fieldnamelist()
.
- Returns an iteration closure function which can be used in the
See luamysql-demo.lua
for more details.
lua-mysql
can also be integrated with C/C++ programs for executing Lua scripts. See host.c
for how to import it into C/C++ environment.
-
If you try to run
luamysql-demo.lua
with the following command:$ lua luamysql-demo.lua
but encounter an error message like this:
lua dynamic libraries not enabled; check your Lua installation
which means you need to re-compile Lua with extra arguments to enable loading dynamic libraries. For example, in Linux systems:
$ make posix MYCFLAGS="-DLUA_USE_DLOPEN -fPIC" MYLIBS=-ldl
-
If there is an error message like this:
multiple Lua VMs detected
you may re-compile the Lua interpreter with option
-Wl,-E
.
Enjoy it!