Thursday, 8 December 2016

Insert new column in sqlite

ALTER TABLE tableName ADD COLUMN newColm {type};

                               (OR)

ALTER TABLE tableName RENAME TO tempOldTable;

Then create the new table with the missing column:

CREATE TABLE tableName (name TEXT, newColm {type} DEFAULT {defaultValue};

Add with the old data:

INSERT INTO tableName (name) SELECT name FROM tempOldTable;

Then delete the old table:

DROP TABLE tempOldTable;


Before check column Exists or not:

-(BOOL)checkColumnExists
{
    BOOL columnExists = NO;

    sqlite3_stmt *selectStmt;

    const char *sqlStatement = "select columnname from yourtable";
    if(sqlite3_prepare_v2(yourDbHandle, sqlStatement, -1, &selectStmt, NULL) == SQLITE_OK)
        columnExists = YES;

    sqlite3_finalize(selectStmt);
    sqlite3_close(yourDbHandle);
    return columnExists;
}

No comments:

Post a Comment