无法从iPhone应用程序中的SQLite数据库中删除数据
问题描述:
我无法从SQlite数据库中删除数据。我创建了Places的数据库。 ,我可以在UItableView中显示它。但是,当我尝试从它删除代码崩溃。无法从iPhone应用程序中的SQLite数据库中删除数据
我得到的错误是
Assertion failure in -[Place deleteFromDatabase], /Desktop/Current_cortes/Classes/Place.m:148
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: failed to prepare statement with message 'library routine called out of sequence'.
这里是哪里程序崩溃的部分。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSLog(@"Rows can be edited");
// Delete the row from the data source.
Place *PlaceObj = [appDelegate.PlacesArray objectAtIndex:indexPath.row];
[appDelegate removePlace: PlaceObj];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
错误发生在与[appDelegate removePlace: PlaceObj];
一行。是
的removePlace
和deleteFromDatabase
的定义如下:
(void) removePlace:(Place *)PlaceObj
{
NSLog(@"remove place called");
//Delete it from the database.
[PlaceObj deleteFromDatabase];
//Remove it from the array.
[PlacesArray removeObject:PlaceObj];
}
-(void) deleteFromDatabase {
if (delete_statment == nil) {
const char *sql = "DELETE FROM places_table WHERE PlaceID=?";
if (sqlite3_prepare_v2(database, sql, -1, &delete_statment, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
sqlite3_bind_int(delete_statment, 1, PlaceID);
int success = sqlite3_step(delete_statment);
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error: failed to save priority with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_reset(delete_statment);
}
我用Google搜索这个问题,并发现它可能是因为我从不同的线程访问数据库。随着代码开始,我可以从数据库中读取它。所以我需要关闭一些东西,以便我可以从数据库中删除对象?
答
你没有连接SQLite Database.Check你的数据库连接。
,谢谢boss ...我关闭了连接之间的连接,所以不可能删除它。非常感谢你.... – amol