0

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

我的代码是这样的,

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,....]?

4

1 回答 1

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
于 2012-05-14T03:22:47.543 回答