簡體   English   中英

無法從數據庫iOS中刪除數據

[英]Unable to remove data from database ios

我有數據庫,但是當我嘗試從數據庫中刪除數據時,什么也沒發生,我該怎么做以確保其有效? 因為當我按下Delete時dta仍在數據庫中

這是代碼:

/file path to database
    -(NSString*)filePath {
        NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
    }

    //open database
    -(void)openDB {

        if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK) {
            sqlite3_close(db);
            NSAssert(0, @"Databese failed to open");
        }

        else {
            NSLog(@"database opemed");
        }


    }


    - (IBAction)del:(id)sender {

        NSString*sql = [NSString stringWithFormat:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName];
        const char* query_stmt = [sql UTF8String];
        sqlite3_stmt*statement;

        sqlite3_prepare_v2(db, query_stmt, -1, & statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
           NSAssert(0, @"database object delete failed");

        } else {
            NSLog(@"No error");

        }
        sqlite3_finalize(statement);
        sqlite3_close(db)

您不能使用DELETE查詢刪除特定的列值。 用於刪除整行。

在此處輸入圖片說明

問題在於以下查詢:

NSString*sql = [NSString stringWithFormat:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName];

更改為:

NSString*sql = [NSString stringWithFormat:@"DELETE FROM summary WHERE key=\"%@\"",customerName];

如果需要刪除行的特定列值,請使用UPDATE查詢。

請查看sqlite文檔以獲取詳細信息

您編寫的所有函數(如檢查文件路徑,opendb)都應在同一函數中發生(可能在del函數內部)。

這就是我要做的:

-(void)updateStatus:(NSString *)queryString {
    NSString    *docsDir;
    NSArray     *dirPaths;
    dirPaths    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir     = [dirPaths objectAtIndex:0];

    strDatabasePath         = [NSString stringWithString:[docsDir stringByAppendingPathComponent:@"bp.sql"]];
    NSFileManager *filemgr  = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: strDatabasePath] == YES)
    {
        const char *dbpath = [strDatabasePath UTF8String];
        if (sqlite3_open(dbpath, &sqlDatabase) == SQLITE_OK)
        {
            const char* beginString = "BEGIN;";
            sqlite3_stmt *compiledstatement;
            sqlite3_prepare_v2(sqlDatabase, beginString, -1, &compiledstatement, NULL);
            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);

            DLog(@"QUERY : %@",queryString);

            const char *selectStatement = [queryString UTF8String];

            sqlite3_prepare_v2(sqlDatabase, selectStatement, -1, &compiledstatement, NULL);
            //sqlite3_bind_text(compiledstatement,1,[statusString UTF8String],-1,SQLITE_TRANSIENT);

            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);


            const char* endString="END;";
            sqlite3_prepare_v2(sqlDatabase, endString, -1, &compiledstatement, NULL);
            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);

            sqlite3_close(sqlDatabase);
        }
        else DLog(@"Failed to open table");
    }
}

NSString *queryString;
queryString = [NSString stringWithFormat:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName];
[self updateStatus:queryString];

希望這可以幫助...

 -(BOOL)DeleteWishList:(int)rowno
{

NSString *queryString=[NSString stringWithFormat:@"delete from wishtable where _id=%d",rowno];

[self openDB];
char *err;
if (sqlite3_exec(dBObject, [queryString UTF8String], NULL,NULL, &err)!= SQLITE_OK)
{
    sqlite3_close(dBObject);    
    return NO;
}   
else 
{
    return YES;
}

}

暫無
暫無

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

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