loadlib error

Posted by Wayen on Sun 28 May 2017 08:53 AM — 3 posts, 17,378 views.

China #0
Today,I test the code which use mysql database , the code is :
-- load the MySQL dll
loadlib ("mysql.dll", "luaopen_luasqlmysql") ()

-- create environment object
env = assert (luasql.mysql())

-- connect to data source
con = assert (env:connect ("databasename", "username", "password", "servername"))

-- empty our table
res = con:execute"DROP TABLE players"

res = assert (con:execute[[
CREATE TABLE players(
name varchar(50),
class varchar(50)
)
]])


-- add a few elements
list = {
{ name="Nick Gammon", class="mage", },
{ name="David Haley", class="warrior", },
{ name="Shadowfyr", class="priest", },
}

for i, p in pairs (list) do
res = assert (con:execute(string.format([[
INSERT INTO players
VALUES ('%s', '%s')]], p.name, p.class)
))
end

-- retrieve a cursor
cur = assert (con:execute ("SELECT * from players" ))

-- print all rows, the rows will be indexed by field names
row = cur:fetch ({}, "a")

while row do
print ("\n------ new row ---------\n")
table.foreach (row, print)

-- reusing the table of results
row = cur:fetch (row, "a")
end

-- close everything
cur:close()
con:close()
env:close()

=========================================================

Then when I run the script , A error occur:

[string "Script file"]:2: attempt to call global 'loadlib' (a nil value)
stack traceback:
[string "Script file"]:2: in main chunk

So I don't think this code is error but maybe my system setting is not correct ?
USA Global Moderator #1
You want package.loadlib instead

https://www.mushclient.com/scripts/doc.php?lua=package.loadlib
China #2
I replace the code use this to test the loadlib:

assert(package.loadlib ("mysql.dll", "luaopen_luasqlmysql")) ()


the old error is disappear .But new error occur :

The specified precedure could not be found .


===========================================================


Then I use dependency waler to check and updated one place:

assert(package.loadlib ("mysql.dll", "luaopen_luasql_mysql")) ()

add a "_"

Now it is correct and not error.