Search code examples
iosdatabasesqlitetransfer

Change Data of database


I'm developing for iOS and I have a previous database really poorly constructed. Now there is already 140 sealed but in my update I make a new database but I can not delete the data... How can I change the data from database A to database B ? I use sqlite3 in xCode 5


Solution

  • Here is an example code altering a column in a database table:

    - (void)alterDB {
    
        sqlite3_stmt *statement;
        sqlite3_stmt *selectStmt;
    
        const char *columnExists = "select callCount from lastCall";
        //Alter the table ONLY IF column we want doesn't exist
        if (sqlite3_prepare_v2(database, columnExists, -1, &selectStmt, NULL) != SQLITE_OK)
        {
            NSString *updateSQL = [NSString stringWithFormat:@"ALTER TABLE lastCall ADD COLUMN callCount INTEGER"];
            const char *update_stmt = [updateSQL UTF8String];
            sqlite3_prepare_v2(database, update_stmt, -1, &statement, NULL);
    
            if (sqlite3_step(statement) == SQLITE_DONE) {
                NSLog(@"DATABASE ALTERED");
            } else {
                NSLog(@"error altering database");
            }
        }
    
    }
    

    I hope this gives you an idea.