删除sqlite数据库中的所有行都不起作用 - iOS

时间:2013-06-03 20:01:45

标签: ios sqlite

我在删除我创建的sqlite表中的所有行时出现问题,我可以根据条件(名称,日期等)从此表中删除但是当我尝试删除表中的所有内容时,它会失败:这是我尝试过的:

-(void) deleteFromDB{
    NSLog(@"delete from DB method being called");

    NSString * deleteQuery = @"DELETE * FROM CLUB";
    [self deleteData:deleteQuery];
}

-(void)deleteData:(NSString *)deleteQuery{
    char *error;
    NSLog(@"Enter deleteData");

    if (sqlite3_exec(clubDB, [deleteQuery UTF8String], NULL, NULL, &error)==SQLITE_OK) {
        NSLog(@"CLUB info deleted");
    }else{
        NSLog(@"Failed");
       NSLog(@"Failed with %s", error);

    }
}

我得到失败的输出。

NSError *error = nil;
    if (![context save:&error]) {
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    }

永远不会打印删除

错误消息是:

X”¿

*更新* * clubDB的类型为sqlite3(在.h中声明为sqlite3 * clubDB

上面更新的错误字符串

** UPDATE2 * * ***

以下是打开/创建数据库的代码:

-(void) createOrOpenDB{
NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docPath = [path objectAtIndex:0];

dbPathString = [docPath stringByAppendingPathComponent:@"club.db"];
NSLog(@"path = %@",dbPathString);

char*error;

NSFileManager*fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dbPathString]){

    const char * dbPath = [dbPathString UTF8String];

    //create db here
    if (sqlite3_open(dbPath, &clubDB)==SQLITE_OK) {
        NSLog(@"success");
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CLUB (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SUBJECT TEXT)";
        sqlite3_exec(clubDB, sql_stmt, NULL, NULL, &error);
        sqlite3_close(clubDB);
    }
}

}

1 个答案:

答案 0 :(得分:3)

您的删除语句无效SQL。要从CLUB删除所有内容,它将是:

delete from CLUB

你不需要明星,因为它显然会影响整个记录。