Luasql 和 SQLite?

我刚开始学习 Lua,作为访问 SQLite DLL 的简便方法,但是在尝试使用 DB-agnostic LuaSQL 模块时遇到了错误:

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

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

print("Hello!")

请注意,我试图从最基本的设置开始,因此在工作目录中只有以下文件,并且 sqlite.dll 实际上是 LuaForge 网站中重命名的 sqlite3.dll:

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

Directory of C:\Temp\luasql
sqlite.dll

我遗漏了某些二进制文件吗,这可以解释错误吗?

谢谢。


编辑:我将 DLL 重命名为其原始的 sqlite3.dll,并更新了源代码以反映此更改(最初是因为在我找到的示例中是这样称呼的)。

此时,代码如下所示…

require "luasql.sqlite3"

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

env:close()

…以及我得到的错误消息:

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

编辑:找到了原因: env = luasql.sqlite3() 而不是 env = luasql.sqlite()。

对于像我这样的新手,这是使用最新的 SQLite LuaSQL 驱动程序 的完整示例:

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()

谢谢。

原文链接 https://stackoverflow.com/questions/2773071

点赞
stackoverflow用户189205
stackoverflow用户189205
  1. 不要重命名 DLL 文件:这会导致 Lua 找不到与 DLL 文件同名的初始化函数。

  2. 不需要 module 调用,只需要 requiremodule 用于创建模块,而不是使用模块。


编辑:根据 LuaSQL 文档,看起来需要调用 luasql.sqlite3() 而不是 luasql.sqlite()

2010-05-05 12:31:28
stackoverflow用户1491
stackoverflow用户1491

在您编辑的片段中,您正在加载luasql.sqlite3模块并尝试访问luasql.sqlite驱动程序。请注意,sqlite3sqlite不同...

2010-05-05 12:59:04