1

我在删除我创建的 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

上面更新的错误字符串

**更新2* * ***

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

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

}
4

1 回答 1

3

您的删除语句不是有效的 SQL。要从 CLUB 中删除所有内容,请执行以下操作:

delete from CLUB

您不需要星号,因为它显然会影响整张唱片。

于 2013-06-03T20:32:30.120 回答