-1

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 !!!

robin
  • 19
  • 1
  • 10

3 Answers3

1

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
robin
  • 19
  • 1
  • 10
freedoo
  • 691
  • 1
  • 5
  • 12
  • i am getting this error lua: check.lua:4: attempt to index local 'db_conn' (a nil value) stack traceback: check.lua:4: in main chunk [C]: ? – robin Jun 27 '14 at 04:14
  • 1
    uh, i think u know the basic example. your database name may be not 'test_db', your database host may be not '192.168.1.3', your name and password may be not 'root' and 'abc123', so u can't get an connect instance. – freedoo Jun 29 '14 at 07:28
0

This code executes well..thank u both @ EgorSkriptunoff &jaylzhang for ur responses

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
robin
  • 19
  • 1
  • 10
0

Try this. It will work with ZeroBraneIDE. Change the interpreter to lua instead of Lua 5.2 or 5.3.

You need to create System DSN on ODBC Data Sources from administrative tool, here it is "testDSN", as shown here:

require "luasql.odbc"
  env = assert(luasql.odbc())
  print(env)
  con = assert(env:connect("testDSN", "sa", "p@ssw0rd"))
  print(con)
  cur = assert (con:execute"use testdb")
  cur = assert (con:execute"SELECT MSISD FROM MSID")
  row = cur:fetch({}, "a")

  while row do
    print(string.format("%s",row.MSISD))
    row = cur:fetch (row, "a")
  end

  cur:close()
  con:close()
  env:close()
Andronicus
  • 25,419
  • 17
  • 47
  • 88
EngBashir
  • 21
  • 4