2011-08-24 136 views
0

我無法從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];一行。是

removePlacedeleteFromDatabase的定義如下:

(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搜索這個問題,並發現它可能是因爲我從不同的線程訪問數據庫。隨着代碼開始,我可以從數據庫中讀取它。所以我需要關閉一些東西,以便我可以從數據庫中刪除對象?

回答

0

你沒有連接SQLite Database.Check你的數據庫連接。

+0

,謝謝boss ...我關閉了連接之間的連接,所以不可能刪除它。非常感謝你.... – amol

相關問題