Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

mysql - Luasql error: "LuaSQL: error connecting to database"

I am working on mysql. I am trying to acess the mysql database using the Luasql.I have installed Luasql using yum. Then i tried the following code:

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

While executing the above code i am getting the following error :

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]: ?

How to overcome this error.Can anyone help me for the proper execution of the code? Thanks !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the variables in env:connect should be variables such as below

local db_conn = env:connect("test_db", "root", "abc123", "192.168.1.3", 3306)
local cur = db_conn:execute("select * from t1")
local row = cur:fetch({}, 'a')
for k, v in pairs(row) do
    print(k, v)
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...