[mysql]Luasql 错误:"LuaSQL︰ 连接到数据库时出错"

标签: MySQL
发布时间: 2017/1/9 22:08:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在对 mysql。我试着访问 mysql 数据库使用 Luasql.I 已安装 Luasql 使用百胜餐饮集团。然后我试着下面的代码︰

mysql = require "luasql.mysql"

env = assert(mysql.mysql())

con = assert(env:connect ( "db_name", "username", "password", "localhost"))

for no, name in rows (con, "select * from t1") do

print (string.format ("%s", name))

end

在执行上述代码时我得到以下错误︰

lua: check.lua:3: LuaSQL: error connecting to database. MySQL: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
stack traceback:
    [C]: in function 'assert'
    check.lua:3: in main chunk
    [C]: ?

如何克服此错误。谁能帮我为适当执行的代码吗?谢谢!!!

解决方法 1:

这段代码执行好。谢谢 @ EgorSkriptunoff & jaylzhang 为您的答复

mysql = require "luasql.mysql"
env = assert(mysql.mysql())
con = assert(env:connect ( "db_name", "username", "password", "hostname", "3306"))
local cur = con:execute("select * from t1")
local row = cur:fetch({}, 'a')
for k, v in pairs(row) do
    print(k, v)
end
赞助商