Reputation: 22489
how can i put my looped value to an array if i have this code?
local data = {
for row in db:nrows("SELECT song.id, song.title as title, artist.name as name FROM song, artist where song.artist_id = artist.id") do
{
song = row.title,
artist = row.name
}
end
}
but i got this error:
unexpected symbol near 'for'
i just wanted to be look like this...
local data = {
{
song = "HI",
artist = "Tom"
}
{
song = "Hello",
artist = "mike"
}
...
}
can anyone can help me about my situation or give some advice about it? thanks in advance
Upvotes: 0
Views: 1527
Reputation: 2091
Having looked at the documentation dbn:rows iterates the rows and returns a table. So the {} are what are causing the problem.
local data = {}
for row in db:nrows("SELECT song.id, song.title as song, artist.name as artist FROM song, artist where song.artist_id = artist.id") do
table.insert(data,row)
end
I have not been able to test the above as I don't have Coruna, and use a different lua system.
Reference: http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki#db_nrows
Upvotes: 2
Reputation: 39223
You would have to do something like this, I think:
result = db:nrows("SELECT song.id, song.title as title, artist.name as name FROM song, artist where song.artist_id = artist.id")
data = {}
for i, row in ipairs(result) do
data[i] = {}
data[i].song = row.title
data[i].artist = row.name
end
Edit: A question though: Why don't you just specify that in the SQL query and use the results as is? I.e.:
data = db:nrows("SELECT song.id, song.title as song, artist.name as artist FROM song, artist where song.artist_id = artist.id")
Upvotes: 3