Luasql 和 SQLite?
我刚刚开始将 Lua 视为访问 SQLite DLL 的简单方法,但在尝试使用与数据库无关的 LuaSQL 模块时遇到了错误:
require "luasql.sqlite"
module "luasql.sqlite"
print("Content-type: Text/html\n")
print("Hello!")
请注意,我正在尝试从最基本的设置开始,因此仅工作目录中有以下文件,sqlite.dll实际上是LuaForge<中重命名的sqlite3.dll< /a> site:
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()
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要重命名DLL文件:这会导致Lua找不到DLL中的初始化函数(与DLL同名)。
您不需要
module
调用,只需require
即可。module
在创建模块时使用,而不是在使用时使用。编辑:根据LuaSQL文档,看起来你需要调用
luasql.sqlite3()
而不是luasql.sqlite()
。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).
You don't need the
module
call, justrequire
.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 ofluasql.sqlite()
.在您编辑的代码片段中,您正在加载 luasql.sqlite3 模块并尝试访问 luasql.sqlite3 驱动程序。请注意,
sqlite3
与sqlite
不同...In your edited snippet you are loading the
luasql.sqlite3
module and trying to access theluasql.sqlite
driver. Note thatsqlite3
is not the same assqlite
...