Home > DeveloperSection > Forums > Adding or Removing objects from SQLite in iOS.
Nigel Bunyan
Nigel Bunyan

Total Post:20

Points:140
Posted on    December-28-2015 5:43 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 165  View(s)
Rate this:

In my SQLite database table I have unique id for each row and have rows name sequentially, like 5 rows in table and having id 0 to 4 for each row.
now, when I remove any row from table like row number 3, this occurs a 'hole' and afterwards I add more data on table but the 'hole' is still exists.
Please tell me that is this important for me to know exact number of row and to have at every row data in order to access my table arbitrarily.
Is there any way in SQLite to do that, and can manage manually to remove and add data.



Tarun Kumar

Total Post:210

Points:1614
Posted on    December-28-2015 10:19 PM

In my knowledge primary keys of database table usually should not change through the lifetime of the row. So, we can find the total number of rows by running:

SELECT COUNT(*) FROM table_name;

Now, create a TRIGGER for that table, whenever a delete creates a hole it will 'roll down' according to ID number:

CREATE TRIGGER table_trigger_name AFTER DELETE ON table_name FOR EACH ROW
BEGIN
UPDATE table_name SET id=id-1 WHERE id > OLD.id;
END;

I hope above example will solve your problem.


Don't want to miss updates? Please click the below button!

Follow MindStick