Using LuaSQL for ODBC Connections

Using ODBC drivers to interact with databases such as MySQL

Installing a MySQL server (optional, when you don't already have one)

  1. Install a mysql server on your machine or on a network.

  2. Run "MySQL 5.5 Command Line Client" from the start menu.

  3. Create a database by typing "CREATE DATABASE myDataBase;" and pressing enter.

Installing and setting up the ODBC driver.

  1. Download and install the MySQL ODBC driver from

  2. Run "Data Sources (ODBC)" from the start menu.

  3. Go to the "System DSN" tab.

  4. Click the "Add" button.

  5. Enter the connection settings for your MySQL database (make sure you made a database first).

  6. The "Data Source Name" is the name you must use when connecting to this database using odbc in lua.

Example code:

env = assert(luasql.odbc())
con = assert(env:connect("My_ODBC_Data_Source_Name"))

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="" },
  { name="Manoel Joaquim", email="" },
  { name="Maria das Dores", email="" },

for i, p in pairs (list) do
  res = assert (con:execute(string.format("INSERT INTO people VALUES ('%s', '%s')",,

-- 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 = cur:fetch (row, "a")-- reusing the table of results

-- close everything
You can confirm that this wrote data to the MySQL database by opening the "MySQL 5.5 Command Line Client" from the start menu and entering
mysql> use myDataBaseName;
mysql> SELECT * FROM people;

This should show the contents written to the database from the lua script.

No Results.

Getting StartedArchitectureBest PracticesHow ToAdvanced TopicsChangelogvrtree_cppCoreForeign Function InterfaceMetanodesMigrationsObserversPropertiesTreeUtilitiesAPI DefinitionsVR ExchangePluginsLua API