1

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

}
26
  • You pass in a buffer for the error message, what does the buffer say? Commented Jun 3, 2013 at 20:20
  • You didn't try printing the error message??? Commented Jun 3, 2013 at 20:20
  • Here's a hint: NSLog(@"Failed with %s", error). Commented Jun 3, 2013 at 20:22
  • the error message is : Failed with X”¿ Commented Jun 3, 2013 at 20:28
  • 1
    Try setting char *error = NULL;. Commented Jun 3, 2013 at 21:33

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply however it is still failing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.