【发布时间】:2011-09-23 15:05:36
【问题描述】:
您好,我正在使用 sqlite,我已成功将数据添加到数据库中,但问题是当我删除其他表视图中的一些数据并导航回我的主视图时,我按下 UIBarButton 调用再次检查数据库中数据的函数。
现在我的问题是新显示在表格中的数据是被删除的数据,所以我尝试打印我的可变数组,在日志中我看到它们不是这样的数据,但在表格视图中也是如此单元格我看到了该数据。下面给出的是我读取数据库中数据的代码
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"PotsDB.sqlite"]; sqlite3 *数据库; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // 设置 SQL 语句并编译它以便更快地访问 const char *sqlStatement = "SELECT potsName FROM potsDetail;"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 而(sqlite3_step(compiledStatement)== SQLITE_ROW){ NSString *Potname=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 0)]; NSString * pname = [[NSString alloc]initWithFormat:@"%@",Potname]; [potsArray addObject:pname]; NSLog(@"数组中的数据是%@",potsArray); } } // 从内存中释放编译好的语句 sqlite3_finalize(compiledStatement); } sqlite3_close(数据库);这也是我在刷新按钮上调用的代码
-(IBAction)RefreshButtonTouched { [potsArray removeAllObjects]; [self Read_Potname_FromDB]; [potsTableView 重新加载数据]; }我正在写的删除查询,我认为我在这里删除有一些错误
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"PotsDB.sqlite"]; sqlite3 *数据库; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // 设置 SQL 语句并编译它以便更快地访问 NSString *deleteStatement = [NSString stringWithFormat:@"从 potsDetail 中删除 potsName = '%@'",sptr]; const char *sqlStatement = [deleteStatement cStringUsingEncoding:NSASCIIStringEncoding]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 而(sqlite3_step(compiledStatement)== SQLITE_ROW){ NSString *Potname=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 0)]; NSString * pname = [[NSString alloc]initWithFormat:@"%@",Potname]; [potsArray addObject:pname]; //[pname 释放]; } } // 从内存中释放编译好的语句 sqlite3_finalize(compiledStatement); } sqlite3_close(数据库);请帮我解决这个问题。提前致谢
【问题讨论】:
标签: iphone objective-c xcode sqlite nsmutablearray