delete row from SQLite database with FMDB

rick picture rick · Feb 28, 2012 · Viewed 10.4k times · Source

i feel like i have read the docs for FMDB a million times. i am not sure why this code is not working - i have tried the executeUpdate method and the executeMethodWithFormat and their corresponding formats, neither seem to work. _dbArray is an array of dictionarys. these lines return the correct values - if i copy and paste the results and run the query in a terminal, it removes the record fine. am i missing something obvious?

- (IBAction)deleteDbEntry:(id)sender {

    // get selected row
    NSInteger row = [_dataTable selectedRow];

    // get the entry number
    NSString *idToDelete = [[_dbArray objectAtIndex:row] valueForKey:@"entryColumn"];

    // open DB for writing
    if (![db open]) {
        [db open];
    }

    [db executeUpdate:@"DELETE FROM tbl1 WHERE entry = ?", idToDelete];

    // close DB
    if ([db open]) {
        [db close];
    }

    [_dataTable reloadData];

    return;
}

UPDATE: i have taken your suggestions and it is still not working, and I am not sure why. the opening and closing of the DB is happening elsewhere now (thank you @ccgus), and I am converting a string to an NSNumber (thanks @bryanmac). the NSLog(@"myNumber is %@", myNumber); call is returning the correct number, and i can confirm that the deletion is not happening. the NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]); is reporting no errors (Error 0: (null)). i have also tried:

[db executeUpdateWithFormat:@"DELETE FROM tbl1 WHERE entry = %@", myNumber];

and

[db executeUpdate:@"DELETE FROM tbl1 WHERE id = ?", [NSNumber numberWithInt:myNumber]];

the first seems like it should work, but i have confirmed it does not delete the row. the second errors at me with "incompatible integer to pointer conversion". what am I missing?

- (IBAction)deleteDbEntry:(id)sender {

    // get selected row
    NSInteger row = [_dataTable selectedRow];

    // get the entry number
    NSString *idToDelete = [[_dbArray objectAtIndex:row] valueForKey:@"entryColumn"];

    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *myNumber = [f numberFromString:idToDelete];
    NSLog(@"myNumber is %@", myNumber);

    [db executeUpdate:@"DELETE FROM tbl1 WHERE entry = ?", myNumber];
    NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);

    [_dataTable reloadData];

    return;
} 

Answer

bryanmac picture bryanmac · Feb 28, 2012

Is your id an int?

If so, try to pass in an NSNumber to the formatted update statement.

This post can convert NSString to NSNumber.

How to convert an NSString into an NSNumber

 [db executeUpdate:@"DELETE FROM tbl1 WHERE entry = ?", idNumber];

Here's a related post:

How to delete a row in a sqlite database table?

Also, make sure you check for errors after your statements:

NSLog(@"Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);