ADDING OR REMOVING OBJECTS FROM SQLITE IN IOS.

Nigel Bunyan

Total Post:20

Points:140
Posted by  Nigel Bunyan
 441  View(s)
Ratings:
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. Tarun Kumar

    Post:214

    Points:1642
    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
    BEGIN
    UPDATE table_name SET id=id-1 WHERE id > OLD.id;
    END;

    I hope above example will solve your problem.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!