现在,我想检查一下这个列是否已经存在于我的表中?
因此如果不存在,我想添加该列与alter table syntex.
目前我正在使用.
-(voID) alterDB{ sqlite3_stmt *statement; NSArray *paths = NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,NSUserDomainMask,YES); Nsstring *documentsDirectory = [paths objectAtIndex:0]; Nsstring *databasePath = [documentsDirectory stringByAppendingPathComponent:DATABASE_name]; if(sqlite3_open([databasePath UTF8String],&database) == sqlITE_OK) { Nsstring *updatesql = [Nsstring stringWithFormat: @"ALTER table diagramInfo ADD ColUMN testColumn VARCHAR"]; const char *update_stmt = [updatesql UTF8String]; sqlite3_prepare_v2(database,update_stmt,-1,&statement,NulL); if(sqlite3_step(statement)==sqlITE_DONE) { UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"DB altered" message:@"Success" delegate:self cancelbuttonTitle:@"OK" otherbuttonTitles:nil]; [alert show]; [alert release]; alert=nil; } else { UIAlertVIEw *alert = [[UIAlertVIEw alloc] initWithTitle:@"DB Updation" message:@"DB not Altered" delegate:self cancelbuttonTitle:@"OK" otherbuttonTitles:nil]; [alert show]; [alert release]; alert=nil; } // Release the compiled statement from memory sqlite3_finalize(statement); sqlite3_close(database); }}解决方法 使用以下功能检查列是否存在.实际上没有检查,所以你可能需要检查语法.概念就像是能够创建准备好的语句,而不是列存在.
-(BOol)checkColumnExists{ BOol columnExists = NO; sqlite3_stmt *selectStmt; const char *sqlStatement = "select yourcolumnname from yourtable"; if(sqlite3_prepare_v2(yourDbHandle,sqlStatement,&selectStmt,NulL) == sqlITE_OK) columnExists = YES; return columnExists;}总结
以上是内存溢出为你收集整理的iphone – 检查如果列已经存在,如果不是在sqlite中改变表全部内容,希望文章能够帮你解决iphone – 检查如果列已经存在,如果不是在sqlite中改变表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)