problem - Writing to SQLite3 from a form in a Lua Page

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

problem - Writing to SQLite3 from a form in a Lua Page

Jasper Cook
Good day

I am trying to learn Lua and friends.  Trying to test a simple,
little browser app, I took subset of test.lp (the one that comes with
Kepler).  It works fine as a form, returning POST values ok.  So, as a
next step, I included code to load the returned POST values into a
little SQLite3 database.  Now, the Lua script loads values into the
SQLite3 just fine as a standalone script, but once I included the database code
in to the Luapage, the following is reported:
//
SQlite3browser.lp:33: attempt to index global 'luasql' (a nil value)
stack traceback:
 SQlite3browser.lp:33: in function 'prog'
 C:\LuaRocks/rocks//cgilua/5.1.3-1/lua/cgilua\lp.lua:132: in function
'include'
 C:\LuaRocks/rocks//cgilua/5.1.3-1/lua/cgilua.lua:282: in function
 (tail call): ?
 (tail call): ?
stack traceback:
 C:\LuaRocks/rocks//cgilua/5.1.3-1/lua/cgilua.lua:161: in function
'err'
 ...:\LuaRocks/rocks//coxpcall/1.13.0-1/lua/coxpcall.lua:24: in
function <...:\LuaRocks/rocks//coxpcall/1.13.0-1/lua/coxpcall.lua:22>
 (tail call): ?
 (tail call): ?
 C:\LuaRocks/rocks//cgilua/5.1.3-1/lua/cgilua.lua:169: in function
'pcall'
 C:\LuaRocks/rocks//cgilua/5.1.3-1/lua/cgilua.lua:629: in function
'main'
 C:\LuaRocks/rocks//wsapi/1.1-2/lua/wsapi\sapi.lua:49: in function
 (tail call): ?
//

I suspect all the messages following the first line will not appear if I
can solve that 'index global 'luasql' issue.

The script follows:
//
#!/usr/bin/env cgilua.cgi

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>CGILua Test Updating SQLite3</title>
</head>

<body>

Form Handling


<p>
Entering values on this form should display them as values in the first
submission

</p>

<form method="post"
   <label>User name : </label><input name="name" maxlength="20"
size="20">
   <label>Age : </label><input name="age" maxlength="3" size="3">
   <input type="submit" value="Post it">
</form>

<p>
The values should show the previous POST
</p>

<p>
Values: Username = <%= cgilua.POST.name or "(not set)"%>, Age = <%=
cgilua.POST.age or "(not set)"%>
</p>

<% require ("luasql.sqlite3") --<----<------<----<--- THIS IS LINE 33
local env = (luasql.sqlite3())
conn = env:connect("luasql-test")
nm = cgilua.POST.name
ag = cgilua.POST.age
assert(conn:execute("insert into people values('" ..nm .."', '" ..ag
.."')"))%>

Date


<p>Today is: <%= os.date() %></p>

</body>
</html>
//

Could someone please explain what I am doing wrong here?  Also, I
thought of pointing to a separate database script by supplying an
"action" and script name to the form, but am unsure how to pass the POST
values to the script in such a case.

Regards
Jasper Cook


Loading...