嗨,我正在使用 sqlite 并且我已成功将数据添加到数据库中,但问题是当我在其他一些 TableView 中删除一些数据并导航回我的主视图时,我按下 UIBarButton 再次调用一个函数检查数据库中的数据。
现在我的问题是新显示在表格中的数据是被删除的数据,所以我尝试打印我的可变数组,在日志中我看到它们没有这样的数据,但是在我看到的表格 View 单元格中也是如此那个数据。下面给出的是我读取数据库中数据的代码
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(数据库);
请帮我解决这个问题。提前致谢
最佳答案
这里可能发生两种情况:
1)数据实际上没有被删除,因为你可能没有调用“sqlite3_finalize(compiledStatement);”声明,负责提交任何事务。
2) UITableView 没有重新加载,你有没有尝试使用“[tableView reloadData];”点击 UIBarbuttonItem?
关于iphone - 从 sqlite 检索数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6495280/