如何在Objective-c中更改Sqlite3中的表?

时间:2016-05-25 09:10:52

标签: ios objective-c iphone sqlite

您好我正在使用Sqlite DB。我正面临一个问题我已成功创建表格现在我需要更改我的表格添加列如下。

-(void) alterDB{
[self open];

sqlite3_stmt *statement;
NSString *updateSQL = [NSString stringWithFormat: @"ALTER TABLE chatTabelHistory ADD COLUMN imagePath TEXT"];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(db, update_stmt, -1, &statement, NULL);
char *error;
if (sqlite3_exec(db, [updateSQL UTF8String], NULL, NULL, &error)!=SQLITE_OK) {

    NSLog(@"Failed to alter =>%s",error);
}
else{
    NSLog(@"DB Created successfully");
}
[self closeDatabase];


}

上面的方法也工作正常,但当我卸载我的应用程序和安装它显示,而我试图插入一行我得到chatTabelHistory has no column named imagePath我的插入方法如下所示

-(void)insert
       [self alterDB];
    NSString *sqlString=[NSString stringWithFormat:@"insert into %@(toid,fromid,msg,timedate,left,messageID,chatStatus,broadCastStreamKey,imagePath) values('%@','%@','%@','%@',%d,'%@','%@','%@','%@')",tableName,userid,friendid,message,timedate,(int)leftright,messageID,chatStatus,broadCastStreamKey,imagePath];

    char *error;
    if (sqlite3_exec(db, [sqlString UTF8String], NULL, NULL, &error)!=SQLITE_OK) {
        [self closeDatabase];
        NSLog(@"Faield to insert chat history %s",error);
    }
    else{
        NSLog(@"chat history inserted successfully");
    }
    }

我想念的地方请帮帮我。

0 个答案:

没有答案