Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...