When I was using luasql
to query a MySQL database, I met a strange error, which, appeared to be caused by mysql_field_count()
's unexpectedly returning 0 for a SELECT
statement.
I have two existing databases A, B and the following lua code:
local conn = assert(env:connect(db, user, passwd, host))
local cur = assert(conn:execute([[
SELECT A.field1, B.field2 from A JOIN B on A.id = B.id
]])
local row = cur:fetch({}, "a")
Now the code failed on the last line, complaining "attempt to index local 'cur' (a number value)". The documentation says execute()
returns a cursor object if there are results, or the number of rows affected by the command otherwise. This is evident in the source of luasql.mysql
. The assertions in my code suggested that both mysql_real_query()
and mysql_store_result()
was successful. So the only explanation to the fact that we ended up with a number instead of a cursor object is that mysql_field_count()
returned 0. But wasn't it a SELECT
statement? Shouldn't it return 2?
Note that this issue occurs rather sporadically. But when it does, there are typically several instances of the same lua script running on the same box. I'm not sure this fact is relevant though.