Posted by
| Nick Gammon
Australia (22,928 posts) bio
Forum Administrator |
Message
| To amplify on my previous answer ...
I found the file odbc.dll from the LuaSQL downloads area (the file name given above). I put that DLL in the same directory as MUSHclient.
I created a test database using Access, and made a system DSN using the ODBC control panel.
Now, the following test code successfully created a table, put data into it, and read the results back:
-- load the ODBC dll
assert (loadlib ("odbc.dll", "luaopen_luasqlodbc")) ()
-- create environment object
env = assert (luasql.odbc())
-- connect to data source
con = assert (env:connect ("luatest", -- DSN name
"nick", -- user name
"swordfish")) -- password
-- empty our table
assert (con:execute"DROP TABLE players")
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
assert (con:execute(string.format([[
INSERT INTO players
VALUES ('%s', '%s')]], p.name, p.class)
))
end -- for loop
-- 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 -- while loop
-- close everything
cur:close()
con:close()
env:close()
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|