robin
robin

Reputation: 19

Error: bad argument #1 to 'insert' (table expected, got nil)

I am trying to connect to a mysql server using LuaSql via a mysql proxy. I try to execute a simple program (db.lua):

require("luasql.mysql")
local _sqlEnv = assert(luasql.mysql())
local _con = nil

function read_auth(auth)
local host, port = string.match(proxy.backends[1].address, "(.*):(.*)")
_con = assert(_sqlEnv:connect( "db_name", "username", "password", "hostname", "3306"))
end 

function disconnect_client()
assert(_con:close())
end 

function read_query(packet)
local cur = con:execute("select * from t1")
myTable = {}
row = cur:fetch(myTable, "a") 
print(myTable.id,myTable.user)
end

This code executes well when I execute it without mysql-proxy. When I am connecting with mysql-proxy, the error-log displays these errors:

mysql.lua:8: bad argument #1 to 'insert' (table expected, got nil) db.lua:1: loop or previous error loading module 'luasql.mysql'

mysql.lua is a default file of LuaSql:

---------------------------------------------------------------------
-- MySQL specific tests and configurations.
-- $Id: mysql.lua,v 1.4 2006/01/25 20:28:30 tomas Exp $
---------------------------------------------------------------------

QUERYING_STRING_TYPE_NAME = "binary(65535)"

table.insert (CUR_METHODS, "numrows")
table.insert (EXTENSIONS, numrows)

---------------------------------------------------------------------
-- Build SQL command to create the test table.
---------------------------------------------------------------------
local _define_table = define_table
function define_table (n)
        return _define_table(n) .. " TYPE = InnoDB;"
end

---------------------------------------------------------------------
-- MySQL versions 4.0.x do not implement rollback.
---------------------------------------------------------------------
local _rollback = rollback
function rollback ()
        if luasql._MYSQLVERSION and string.sub(luasql._MYSQLVERSION, 1, 3) == "4.0" then
                io.write("skipping rollback test (mysql version 4.0.x)")
                return
        else
                _rollback ()
        end
end

Upvotes: -1

Views: 3811

Answers (1)

Oliver
Oliver

Reputation: 29453

As stated in my previous comment, the error indicates that table.insert (CUR_METHODS, ...) is getting a nil as first arg. Since the first arg is CUR_METHODS, it means that this object CUR_METHODS has not been defined yet. Since this happens near top of the luasql.mysql module, my guess is that the luasql initialization was incomplete, maybe because the mysql DLL was not found. My guess is that the LUA_CPATH does not find the MySQL DLL for luasql, but I'm surprised that you wouldn't get a package error, so something odd is going on. You'll have to dig into the luasql module and C file to figure out why it is not being created.

Update: alternately, update your post to show the output of print("LUA path:", package.path) and print("LUA path:", package.cpath) from your mysql-proxy script and also show the path of folder where luasql is installed and contents of that folder.

Upvotes: 1

Related Questions