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

时间:2013-02-12 10:59:39

标签: iphone ios database sqlite ios6

我有数据库,但是当我试图从数据库中删除数据时没有任何反应,我该怎么做以确保它有效?因为当我按下删除时,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)

3 个答案:

答案 0 :(得分:3)

您无法使用DELETE查询删除特定列值。这是为了删除整行。

enter image description here

问题在于以下查询:

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 documentation了解详情

答案 1 :(得分:0)

您编写的所有函数(如检查filepath,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];

希望这会有所帮助......

答案 2 :(得分:0)

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

}