从sqlite中获取数据的问题
您好我正在使用sqlite,并且我已经成功地将数据添加到数据库中,但问题是,当我删除某些其他表视图中的一些数据并导航回到我的主视图和我按UIBarButton调用一个函数,再次检查数据库中的数据。从sqlite中获取数据的问题
现在我的问题是,在表中新显示的数据是被删除的数据,所以我试图打印我的可变数组,并在日志中我看到他们是没有这样的数据,但在表中也是如此查看单元格我看到的数据。下面给出的是我的代码在db
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"PotsDB.sqlite"]; sqlite3 *database; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // Setup the SQL Statement and compile it for faster access const char *sqlStatement = "SELECT potsName FROM potsDetail;"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { while(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(@"The data in the array is %@",potsArray); } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); } sqlite3_close(database);
而且继承人的代码,我上刷新按钮
-(IBAction)RefreshButtonTouched { [potsArray removeAllObjects]; [self Read_Potname_FromDB]; [potsTableView reloadData]; }
删除查询中,我正在写打电话来读取数据,我想我是在这里删除一些错误
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"PotsDB.sqlite"]; sqlite3 *database; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // Setup the SQL Statement and compile it for faster access NSString *deleteStatement = [NSString stringWithFormat:@"delete from potsDetail where potsName = '%@'",sptr]; const char *sqlStatement = [deleteStatement cStringUsingEncoding:NSASCIIStringEncoding]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { while(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 release]; } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); } sqlite3_close(database);
请帮我解决这个问题。在此先感谢
有两种情况可以发生在这里:
1)数据实际上没有删除,因为你可能不叫“sqlite3_finalize(compiledStatement);”声明,它负责提交任何事务。 2)UITableView不重载,你尝试使用“[tableView reloadData];”点击UIBarbuttonItem?
你从来没有真正删除sqlite数据库中的数据。 [potsArray removeAllObjects]将删除内存中的版本,而不是存储在磁盘上的版本。你将不得不添加一个DELETE FROM查询的数据库方法来实际影响sqlite数据库。
将发布我的删除代码 – Radix
是的我试过了,你可以在上面的代码中看到我已经调用了sqlite3_finalize(compiledStatement); – Radix
现在,有可能的UITableViewCell,你在(cell == nil)范围内设置标签文本吗?我认为,问题是在tableview上显示标签 – iMOBDEV
是的,我只是这样做的 – Radix