user2366782
user2366782

Reputation:

Delete all rows in sqlite database not working - iOS

Im having an issue deleting all the rows in a sqlite table I created, I can delete from this table based on a condition, (name, date etc) however when I try to delete all the contents in the table it fails: Here is what I've tried:

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

    }
}

I get an output of failed.

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

cant delete is never printed

The error message is:

X”¿

*Update** clubDB is of type sqlite3 (declared in .h as sqlite3*clubDB

error string updated above

**Update2*****

Here is the code for opeing/creating the database:

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

}

Upvotes: 1

Views: 1018

Answers (1)

Stephen Darlington
Stephen Darlington

Reputation: 52565

Your delete statement isn't valid SQL. To delete everything from CLUB it would be:

delete from CLUB

You don't need the star as it obviously affects the whole record.

Upvotes: 3

Related Questions