为什么这个 Lua 脚本中的 MySQL 查询失败呢?

我有一个小游戏,它使用了 LuaJIT 以及 LuaPower 的 MySQL 客户端库。

现在,我已经成功建立了连接,并且还能够选择表格,但是我无法将任何数据插入表格中。

错误信息如下:

FATAL ERROR [UNHANDLED EXCEPTION]: Attempt to call uninitialized function pointer

这是我的 Lua 代码,以便你能够理解我试图做什么:

addhook("join", "onJoin")
addhook("minute", "saveStats")
addhook("say", "logChat")
addhook("say","onSay")

local mysql = require'mysql'

-- MySQL 连接详情:
sql_host = "localhost" -- 主机名
sql_user = "root" -- 用户名
sql_password = "1c%CIuROpD17" -- 密码
sql_db_name = "cs2d" -- 数据库名
sql_port = 3306

local con = mysql.connect(sql_host, sql_user, sql_password, sql_db_name, 'utf8')

function onJoin(id)
local usgn = player(id, "usgn")
local ip = player(id, "ip")
local name = player(id, "name")
local datime = os.date("%c")

     if usgn > 0 then
          -- con:query("INSERT INTO `logins`(`col_int`,`col_varchar`,`col_varchar`,`col_varchar`,`col_datetime`) VALUES (usgn, ip, name, datime)")
          print("a")
          msg("new player joined!")

     elseif usgn == 0 then
         -- con:query("INSERT INTO `logins`(`col_int`,`col_varchar`,`col_varchar`) VALUES (name, datime)")
     end

end

不幸的是,查询似乎不是正确的格式,或者至少没有真正的线索可以让我解决问题。

我无法进一步调试它。但我相信一定与我尝试进行查询的方式有关,因为例如,使用相同的连接选择表格都是成功的。

点赞
用户1442917
用户1442917

原文

It's unlikely that you get this error as the result of the SQL query. Since you don't provide a complete example, it's difficult to say what's going on, but I'd start with simplifying the code and figuring out where exactly the error is thrown (for example, you don't check the return value from mysql.connect call, which may return nil).

翻译

这个错误不太可能是 SQL 查询的结果。由于您没有提供完整的示例,很难说发生了什么,但我会从简化代码并找出错误的确切位置开始(例如,您没有检查 mysql.connect 调用的返回值,它可能会返回 nil)。

2019-11-18 03:52:56