Luasql 和 SQLite?

发布于 2024-08-31 01:52:30 字数 1615 浏览 1 评论 0原文

我刚刚开始将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-09-07 01:52:30
  1. 不要重命名DLL文件:这会导致Lua找不到DLL中的初始化函数(与DLL同名)。

  2. 您不需要 module 调用,只需 require 即可。 module 在创建模块时使用,而不是在使用时使用。


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

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

一片旧的回忆 2024-09-07 01:52:30

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

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文