2012-05-14 4 views
0

en affichant un tableau dans lua, il commence toujours par 1, donc je l'ai utilisé dans ma requête sql si j'utilise select * from ... comme référence de table.id. mon problème est maintenant ce que si la requête sql de table.id ne commencera pas avec 1, ou ce sera comme [3,5,6, ...]?tableau indexation dans lua

mon code est comme ça,

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 

de sorte que la sortie est row.id [3,5,6, ..] parce que j'utilisé le row.id comme l'indice du tableau data.

ma question est que dois-je faire ou utiliser de sorte que l'indice du tableau data deviendra comme ceci, [1,2,3, ....]?

Répondre

2

Vous pouvez simplement utiliser une variable d'index:

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