Adding or Removing objects from SQLite in iOS.

Total Post:20


 840  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.

  1. Re: Adding or Removing objects from SQLite in iOS.

    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 >;

    I hope above example will solve your problem.


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.