Login
or
register
Overview
Overview
Why Lua?
Vision
Uses
Modules
General FAQ
Credits
License
Getting Started
Installation
Hello World
Databases
Web Servers
More Examples
Reference
FAQ
Getting Help
Status
News
Road Map
Releases
Site Edits
Community
Overview
Architecture
Detailed Road Map
Source
Builds
Bugs
FAQ
Dev Team
Mailing List
Using a Database
Anonymous users must enter
captcha
below.
Don't put anything here
Page Parameters
Page Name (for URL)
Page Title
Show Advanced Options
Category
Prototype
Don't put anything here
Page Content
There is a few libraries to access databases with Lua out there, but only [LuaSQL](http://www.keplerproject.org/luasql/)provides uniform access to different databases. Here is a simple example of how to use LuaSQL: -- Initialization (1) require"luasql.postgres" local env = luasql.postgres () local conn = assert (env:connect ("lpg")) -- Building SQL statement (2) course_list = { "Music", "Literature", } for i, course in ipairs (course_list) do course_list[i] = "'"..course.."'" end local c_list = table.concat (course_list, ",") local stmt = [=[ select a.id, a.name from alumn a inner join course c on (a.course_id = c.id) where c.name in (]=]..c_list..")" -- SQL execution and error handling (3) local cur, err = conn:execute (stmt) if not cur then error (err.." SQL = [=["..stmt.."]=]") end -- Iteration loop (4) local id, name = cur:fetch () while id do cgilua.put (string.format ("<tr><td>id = %%s<td>name = %%s</tr>", id, name)) id, name = cur:fetch () end However, it would be better to define an iterator in order to use it inside a LuaPage: function lines (stmt) local cur = assert (conn:execute(stmt)) return function () return cur:fetch() end end <table> <%% for id, name in lines"select id, name from alumn" do %%> <tr> <td><%%= id %%> <td><%%= name %%> </tr> <%% end %%> </table>
Don't put anything here
About this Edit
Minor Edit
Don't put anything here
Edit Summary
Don't put anything here
Don't put anything here
Powered by
Sputnik
|
XHTML 1.1