CodeGuy
CodeGuy

Reputation: 28907

Question about SQLite3 and updating iPhone app

Consider the following scenario.

I have an iPhone app in the app store which performs queries on a 16 column SQLite3 table. As an update, I want to add a new column to this table.

When someone installs the update, will it mess up? Will the table get replaced? How does this work. How should I handle this?

Upvotes: 4

Views: 1202

Answers (2)

David Hodge
David Hodge

Reputation: 1764

FYI if you never copied the database to make it editable, it will be replaced. If you copied the DB to somewhere that is writeable, then you have to handle the replacing yourself.

Upvotes: 1

Radu Lucaciu
Radu Lucaciu

Reputation: 706

One way to do it would be to check at launch time if the table has the new field. You can use the following SQLite command to retrieve all fields from a table

PRAGMA table_info(table-name);

If the field you're looking is not in the result set, execute an ALTER TABLE table-name ADD COLUMN column-name column-type to update your table.

You can use this method to loop through the fields of a table

- (BOOL)getFields
{
    const char *sql = "PRAGMA table_info(myTable)";
    sqlite3_stmt *stmt;

    if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        return NO;
    }

    while(sqlite3_step(stmt) == SQLITE_ROW)
    {
        NSString *fieldName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
        NSLog(@"%@", fieldName);
    }

    return YES;
}

Hope this helps

Upvotes: 4

Related Questions