In an SQLite table in my app, I need to have a column with row numbers going 1,2,3,4,5 (or to be able to get a row number for every row). Now I delete rows and add rows all the time, so I cant just put a number every time I add a new row, because when rows are deleted, the numbering will get messed up. So is there a way to get the row number, or create a special column that will act as row numbering? If so, how?
Share
If you want this to be reset every time you add and delete rows, should this really be a column in your table?
For example, if you just want to be able to select the 20th row from the table, you can use the
LIMITandOFFSETclauses of SQL to select a particular row, e.g., to retrieve the 20th row from mynewstable, I could do:Alternatively, if you really want a column with this row number information, you could just add a new
INTEGERcolumn type and then write an Objective C method to update the values for that column, and invoke that method any time you delete or insert rows in your result set.There are a number of ways of solving your problem. You have to explain what you’re trying to accomplish for us to help you out. The typical
AUTOINCREMENTanswer is what we generally use for table keys, but it sounds like you don’t want this to be the key of your table, but rather something else.