简体   繁体   中英

Unable to delete data from SQLite database in iphone application

I am not able to delete data from SQlite database. I have created a database of Places. and I am able to display it in UItableView. But when I try to delete something from it code crashes.

The error I am getting is

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'.

Here is the the part where program crashed.

- (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.
    }   
}

The error is occurring on the line with [appDelegate removePlace: PlaceObj]; .

The definitions of removePlace and deleteFromDatabase are as follows:

(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);
}

I googled for the problem and found that it might be because I am accessing database from different threads. As thecode starts I am able to Read it from database. so Do I need to close something so that I can delete objects from database?

您尚未与SQLite数据库连接。请检查数据库连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM