2012-05-14 42 views
0

在lua中显示数组时,它总是以1开头,所以如果我使用select * from ...作为table.id的引用,则在我的sql查询中使用它。我现在的问题是如果table.id的sql查询不会以1开头,或者它会像[3,5,6, ...]那样?lua中的数组索引

我的代码是这样的,

local data = {} 

for row in db:nrows("SELECT song.id as id, song.title as song, artist.name as artist, genre.name as genre, album.title as album FROM song, artist, genre, album WHERE song.artist_id = artist.id AND song.genre_id = genre.id AND song.album_id = album.id AND song.duration = 120.00") do 
         data[row.id] = {} 
         data[row.id].song = row.song 
         data[row.id].artist = row.artist 
         data[row.id].genre = row.genre 
         data[row.id].album = row.album 
end 

这样的row.id输出是[3,5,6,..]因为我使用的row.id作为数组data的索引。

我的问题是我应该怎么做或使用,以便阵列data的索引将变成这样,[1,2,3,....]?

回答

2

你可以只使用一个索引变量:

local data = {} 
local next = 1 

for row in db:nrows("SELECT song.id as id, song.title as song, artist.name as artist, genre.name as genre, album.title as album FROM song, artist, genre, album WHERE song.artist_id = artist.id AND song.genre_id = genre.id AND song.album_id = album.id AND song.duration = 120.00") do 
    data[next] = row 
    next = next + 1 
end