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