SQLITE Selecting Rows

I’m a bit lost. I have this function below in its own LUA model, and the database is open. When I run this now, I get attempt to index global row. This did work at first. Is this a scope issue? Just not sure why I am seeing this.

function returnUnfinishedPuzzleCount()

sqlString=“SELECT COUNT(*) as INCOMPLETE FROM PUZZLEDATA WHERE COMPLETE=‘N’;”

for row in db:nrows(sqlString) do

print (row.INCOMPLETE)

end

return row.INCOMPLETE

end

Hi,

When you say the database is open, I assume you mean in another part of your program?

If so, then this module does not have any reference to that database.

You have 2 different options that I can suggest:

  1. Pass a reference of your “db” to your module method:

[lua]

function returnUnfinishedPuzzleCount( db ) --pass ref into method

  --rest of method

end

[/lua]

  1. Open the connection in the module instead:

[lua]

–Your Module

local mymod = {}

local sqlite3 = require( “sqlite3” )

local db = sqlite3.open( path )

function mymod.returnUnfinishedPuzzleCount()

  --rest of method

end

return mymod

[/lua]

Hope that helps.

Cheers.

Specially what’s wrong with your code is how Lua handles index variables.  This block of code:

for row in db:nrows(sqlString) do print (row.INCOMPLETE) end return row.INCOMPLETE

is really more like:

for local row in db:nrows(sqlString) do -- row is local to the for block. print (row.INCOMPLETE) end -- row doesn't exist here, it only exists inside the for loop. return row.INCOMPLETE

Now of course you can’t put the word local in the for loop but Lua effectively does. Your syntax is right, but row is local to the block and any context to that variable doesn’t exist outside the block.

duh, thanks Rob. Should have caught that. Too many hours in front of screen.

Hi,

When you say the database is open, I assume you mean in another part of your program?

If so, then this module does not have any reference to that database.

You have 2 different options that I can suggest:

  1. Pass a reference of your “db” to your module method:

[lua]

function returnUnfinishedPuzzleCount( db ) --pass ref into method

  --rest of method

end

[/lua]

  1. Open the connection in the module instead:

[lua]

–Your Module

local mymod = {}

local sqlite3 = require( “sqlite3” )

local db = sqlite3.open( path )

function mymod.returnUnfinishedPuzzleCount()

  --rest of method

end

return mymod

[/lua]

Hope that helps.

Cheers.

Specially what’s wrong with your code is how Lua handles index variables.  This block of code:

for row in db:nrows(sqlString) do print (row.INCOMPLETE) end return row.INCOMPLETE

is really more like:

for local row in db:nrows(sqlString) do -- row is local to the for block. print (row.INCOMPLETE) end -- row doesn't exist here, it only exists inside the for loop. return row.INCOMPLETE

Now of course you can’t put the word local in the for loop but Lua effectively does. Your syntax is right, but row is local to the block and any context to that variable doesn’t exist outside the block.

duh, thanks Rob. Should have caught that. Too many hours in front of screen.