2012-05-11 69 views
0

指數如何,我可以把我的循環值到一個數組,如果我有這樣的代碼?在LUA循環如何與陣列

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 
} 

,但我得到這個錯誤:

unexpected symbol near 'for' 

我只是想成爲這個樣子......

local data = { 
{ 
    song = "HI", 
    artist = "Tom" 
} 
{ 
    song = "Hello", 
    artist = "mike" 
} 
... 
} 

可有人能幫助我我的情況或提供一些意見關於它? 由於事先

回答

3

你將不得不做這樣的事情,我想:

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 

編輯:雖然一個問題:爲什麼你不指定在SQL查詢和使用結果是? 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") 
+0

萊納斯感謝你的答案...你的問題,我只是想爲'tableView.lua'我電暈SDK數組.. – gadss

2

查看了文檔dbn:rows迭代行並返回一個表。所以{}是導致問題的原因。

本地數據= {}

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 

我還沒有能夠測試如我沒有拉科魯尼亞以上,並使用不同的LUA系統。

參考:http://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki#db_nrows