2012-05-11 23 views
0

このコードがある場合、どのようにループされた値を配列に配置できますか?forループinループ配列を使用してインデックスを作成する方法

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クエリでそれを指定して使用していませんそのままの結果ですか? :

data = db:nrows("SELECT song.id, song.title as song, artist.name as artist FROM song, artist where song.artist_id = artist.id") 
+0

おかげLinusが...あなたの質問については、私はちょうど私のコロナSDKの 'tableView.lua'の配列をしたいです。.. – 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 

Iはコルーニャを有し、異なるLUAシステムを使用しないように私は上記をテストすることができていません。

参考:あなたの答えのためのhttp://lua.sqlite.org/index.cgi/doc/tip/doc/lsqlite3.wiki#db_nrows

関連する問題