problem with mysql and lua and mushclient

Posted by Zl on Sun 24 Jan 2010 04:24 PM — 5 posts, 17,366 views.

#0
I am so happy that you are still developing mushclient.
I have problem with database and lua and mushclient.
I write program juas like what you metioned in a quite old post.
------------------------------------------------------
function database()
-- 载入 ODBC 外部扩展库
assert (package.loadlib ("odbc.dll", "luaopen_luasqlodbc")) ()

-- 创建环境对象
env = assert (luasql.odbc())

-- 连接数据库
con = assert (env:connect ("xx", -- DSN 名称
"xxxx", -- 用户名
"xxxxxx")) -- 密码

-- empty our table
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()
end
-------------------------------------------------
when I execute this function in mushclient( /database() )there's no result in the mushclient interface.
I checked the database and found that the table is right there.
what is the problem?
----------------------------------------------------
lua5.1
odbc.dll
mysql5.1
Amended on Sun 24 Jan 2010 04:33 PM by Zl
Australia Forum Administrator #1
It worked fine for me, once I put in a valid DSN name, username and password. When you said "the table is right there" did it have the data in it?

You need to set up the ODBC control panel for this to work. It might be easier to use mySQL, or use the new Sqlite3 interfaces which are now in MUSHclient.
#2
I am using mysql and also setup the ODBC,however the table in database is ok and there's three row in the table.I am sure that the table is build by the program, so I think both odbc and mysql work well.
I execute "select * from players" via mysql command line client and the result is ok.
so I think the problem may cause by this code:
cur = assert (con:execute ("SELECT * from players" ))
--------------------------------------------------
I tried to show some result with Note()
cur = assert (con:execute ("SELECT * from players" ))
Note(cur) --result:ODBC cursor (00DE6BF8)
p=cur:getcolnames()
Note(p[1]) --result:name
row = cur:fetch (row, "a")
Note(row[1]) --result:[string "Script file"]
--:164: attempt to index global 'row'
--(a nil value)
---------------------------------------------------
this is result from mysql client:
mysql> select * from players;
+-------------+---------+
| name | class |
+-------------+---------+
| Nick Gammon | mage |
| David Haley | warrior |
| Shadowfyr | priest |
+-------------+---------+
3 rows in set (0.81 sec)

mysql>
--------------------------------------------------
by the way,I am using mushclient 4.18
Amended on Mon 25 Jan 2010 06:58 AM by Zl
#3
I tried mushclient 4.43 with mysql.dll and libmysql.dll
the problem was solved.
but I still want to know why it failed when I use 4.18 with odbc.dll?
as I mentioned in the last post,the table can be created successfully means the odbc and mysqlserver is ok,isn't it?
Australia Forum Administrator #4
Well I don't know for sure. Version 4.18 is quite a few versions ago (like, 29) and was released about 2.5 years ago. Maybe in the release notes there is some reference to what the problem might be, but as a general rule, if software fails, try the latest version.