簡體   English   中英

無法從iPhone應用程序中的SQLite數據庫中刪除數據

[英]Unable to delete data from SQLite database in iphone application

我無法從SQlite數據庫中刪除數據。 我創建了一個地方數據庫。 我可以在UItableView中顯示它。 但是,當我嘗試從中刪除某些內容時,代碼崩潰了。

我得到的錯誤是

-[Place deleteFromDatabase],/ Desktop / Current_cortes / Classes / Place.m中的斷言失敗:148

由於未捕獲的異常“ NSInternalInconsistencyException”而終止應用程序,原因:“錯誤:無法編寫帶有消息”庫例程的調用順序錯誤”的語句。

這是程序崩潰的部分。

- (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];的行發生錯誤[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);
}

我搜索了這個問題,發現可能是因為我正在從不同的線程訪問數據庫。 當代碼開始時,我能夠從數據庫中讀取它。 所以我需要關閉某些東西以便從數據庫中刪除對象嗎?

您尚未與SQLite數據庫連接。請檢查數據庫連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM