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

Total Post:20

Posted on    December-28-2015 5:43 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 383  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:214

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
UPDATE table_name SET id=id-1 WHERE id > OLD.id;

I hope above example will solve your problem.

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

Follow MindStick