简体   繁体   English

无法从数据库iOS中删除数据

[英]Unable to remove data from database ios

I have database, but when i tried to remove data from the database nothing happens, what should i do to make sure it works? 我有数据库,但是当我尝试从数据库中删除数据时,什么也没发生,我该怎么做以确保其有效? Because when i pressed delete the dta is still in the database 因为当我按下Delete时dta仍在数据库中

This is the code: 这是代码:

/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)

You can't delete specific column values using the DELETE query. 您不能使用DELETE查询删除特定的列值。 It's for removing the entire row. 用于删除整行。

在此处输入图片说明

The problem is with the following query: 问题在于以下查询:

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

Change it to: 更改为:

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

If you need to remove particular column value of a row use the UPDATE query. 如果需要删除行的特定列值,请使用UPDATE查询。

Please check the sqlite documentation for the details 请查看sqlite文档以获取详细信息

All the functions that you wrote like checking filepath, opendb should occur in the same function(maybe inside your del function). 您编写的所有函数(如检查文件路径,opendb)都应在同一函数中发生(可能在del函数内部)。

This is how I will do it: 这就是我要做的:

-(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];

Hope this helps... 希望这可以帮助...

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