How to assign sqlite query results to table/array and then use later?

If I have a sqlite query that generates say 100 rows of data, how can I assign this to an array and iterate through it late?.  I am aware of db:nrows, but I’m not sure how to use that later in my app after the query is over, since it seems that I need to loop through that as I execute the query. I want to save this output into an array and be able to iterate through it later in my app.

So, if I have a select query that brings back 100 rows, with columns ID, Name, Address, State, Zip then how do I assign it to an array and use it later in my app?

Thanks!

Hi,

In the following example I iterate through a table called “puzzleList” and store 2 rows in an array “gameDetails”.

[lua]

local gameDetails = {}

for row in db:nrows([[SELECT * FROM “puzzleList”]]) do

        gameDetails[#gameDetails + 1] = {imageName = row.imageName, dateCreated = row.dateCreated}

end

[/lua]

I could then later use this array as I would usually.

e.g.

[lua]

for i = 1, #gameDetails do

    print (gameDetails[i].imageName)

end

[/lua]

Hope this helps.

Craig

Brilliant!  Thanks so much, Craig!

Hi,

In the following example I iterate through a table called “puzzleList” and store 2 rows in an array “gameDetails”.

[lua]

local gameDetails = {}

for row in db:nrows([[SELECT * FROM “puzzleList”]]) do

        gameDetails[#gameDetails + 1] = {imageName = row.imageName, dateCreated = row.dateCreated}

end

[/lua]

I could then later use this array as I would usually.

e.g.

[lua]

for i = 1, #gameDetails do

    print (gameDetails[i].imageName)

end

[/lua]

Hope this helps.

Craig

Brilliant!  Thanks so much, Craig!