我正在尝试遵循在此处找到的 luasql 中使用准备好的语句的示例:http: //lists.luaforge.net/pipermail/kepler-project/2008-January/002207.html
我安装了 luasql 2.2.0 版。
当我尝试示例代码时,我收到以下错误消息:
lua: postgrestest.lua:63: 尝试调用方法 'gettypes' (一个 nil 值) 堆栈回溯: postgrestest.lua:63: in main chunk [C]: ?
这是代码:
require "luasql.postgres"
local databasename = "test"
local databaseUser = "user"
local databasepassword = "password"
local databaseserver="10.10.10.10" -- only rqd for remote database servers.
--local databaseserver="127.0.0.1"
local databaseport = 5432 -- only rqd for remote database servers.
env = assert (luasql.postgres())
con = assert (env:connect(databasename, databaseUser, databasepassword, databaseserver))
-- DROP ANY EXISTING PEOPLE TABLE
-- note: con:execute() Returns: a cursor object if there are results, or the number of rows affected by the command otherwise.
res = con:execute("DROP TABLE people")
-- RECREATE PEOPLE TABLE
res = assert (con:execute[[
CREATE TABLE people(
id integer,
fname text,
lname text,
job text
)
]])
print ('result from create table is:'..res)
-- ADD SOME PEOPLE TO THE PEOPLE TABLE
res = assert(con:execute("INSERT INTO people " ..
"VALUES (1, 'Roberto', 'Ierusalimschy', 'Programmer')"), "record inserted")
print ("result from insert"..res)
res = assert(con:execute("INSERT INTO people " ..
"VALUES (3, 'Taylor', 'Swift', 'Singer')"))
-- RETRIEVE THE PEOPLE TABLE SORTED BY LAST NAME INTO CURSOR
cur = assert (con:execute"SELECT * from people order by lname")
-- LOOP THROUGH THE CURSOR AND PRINT
print()
print(string.format("%15s %-15s %-15s %-15s",
"#", "FNAME", "LNAME", "JOB"))
print(string.format("%15s %-15s %-15s %-15s",
"-", "-----", "-----", "---"))
row = cur:fetch ({}, "a")
while row do
print(string.format("%15d %-15s %-15s %-15s",
row.id, row.fname, row.lname, row.job))
row = cur:fetch (row, "a")
end
print()
-- prepared statements
types = con:gettypes()
stmt, err = con:prepare("select * from people where lname=$1", types["text"])
if err then
print("problems creating prepared statement"..err)
else
cur, err = stmt:execute("Swift")
if err then
print("Couldn't execute the statement: "..err);
else
results = cur:fetch({}, "a")
while results do
print(results.id)
end
end
end
-- CLOSE EVERYTHING
cur:close()
con:close()
env:close()
我还尝试将 prepare 语句更改为如下所示:
--types = con:gettypes()
--stmt, err = con:prepare("select * from people where lname=$1", types["text"])
stmt, err = con:prepare("select * from people where lname=$1", "Swift")
if err then
print("problems creating prepared statement"..err)
else
--cur, err = stmt:execute("Swift")
cur, err = stmt:execute()
if err then
print("Couldn't execute the statement: "..err);
但是我得到了同样的错误信息,除了使用prepare方法:
lua: postgrestest.lua:65: attempt to call method 'prepare' (a nil value)
stack traceback:
postgrestest.lua:65: in main chunk
[C]: ?
你能告诉我我做错了什么/如何解决这个问题吗?谢谢!
编辑 1
与数据库的连接和所有其他代码都有效。循环遍历表格并打印数据的代码有效。
谢谢。