5

I just got started looking at Lua as an easy way to access the SQLite DLL, but I ran into an error while trying to use the DB-agnostic LuaSQL module:

require "luasql.sqlite"
module "luasql.sqlite"

print("Content-type: Text/html\n")

print("Hello!")

Note that I'm trying to start from the most basic setup, so only have the following files in the work directory, and sqlite.dll is actually the renamed sqlite3.dll from the LuaForge site:

Directory of C:\Temp
<DIR> luasql
lua5.1.exe
lua5.1.dll
hello.lua

Directory of C:\Temp\luasql
sqlite.dll

Am I missing some binaries that would explain the error?

Thank you.


Edit: I renamed the DLL to its original sqlite3.dll and updated the source to reflect this (originally renamed it because that's how it was called in a sample I found).

At this point, here's what the code looks like...

require "luasql.sqlite3"

-- attempt to call field 'sqlite' (a nil value)
env = luasql.sqlite()

env:close()

... and the error message I'm getting:

C:\>lua5.1.exe hello.lua
lua5.1.exe: hello.lua:4: attempt to call field 'sqlite' (a nil value)

Edit: Found what it was: env = luasql.sqlite3() instead of env = luasql.sqlite().

For newbies like me, here's a complete example with the latest SQLite LuaSQL driver:

require "luasql.sqlite3"

env = luasql.sqlite3()
conn = env:connect("test.sqlite")

assert(conn:execute("create table if not exists tbl1(one varchar(10), two smallint)"))
assert(conn:execute("insert into tbl1 values('hello!',10)"))
assert(conn:execute("insert into tbl1 values('goodbye',20)"))

cursor = assert(conn:execute("select * from tbl1"))
row = {}
while cursor:fetch(row) do
    print(table.concat(row, '|'))
end

cursor:close()
conn:close()

env:close()

Thank you.

Gulbahar
  • 5,343
  • 20
  • 70
  • 93
  • Why is the DLL renamed? What error are you getting? – Matthew Flaschen May 05 '10 at 12:25
  • The example given above will give the following error with Lua 5.1.4: `lua: sql.lua:3: attempt to index global 'luasql' (a nil value)`. This is because `require` no longer defines globals. Changing the first line to `luasql = require "luasql.sqlite3"` will solve the problem. – user1704650 Oct 24 '12 at 19:04

2 Answers2

2
  1. Don't rename the DLL file: This will cause Lua not to find the initialization function in the DLL (which is named the same as the DLL).

  2. You don't need the module call, just require. module is used when you are creating a module, not when you use it.


Edit: According to the LuaSQL documentation, it looks like you need to call luasql.sqlite3() instead of luasql.sqlite().

interjay
  • 107,303
  • 21
  • 270
  • 254
0

In your edited snippet you are loading the luasql.sqlite3 module and trying to access the luasql.sqlite driver. Note that sqlite3 is not the same as sqlite...

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99