Search code examples
iphonesqlitedelete-row

Can't delete row from SQLite database, yet no errors are issued


I am using what, according to my Xcode debugger, is proper syntax to delete a row from a table in my project. However, when I go back to check my database, it still exists. My other SQL syntax for inserting entries is correct, so I'm not sure what I'm doing wrong. NSLogs confirm both variables are being sent correctly:

    -(void) deleteSelectedRowFromTable: (NSString *) tableName cityName:(NSString *)city 
{

[self openDB];
NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM %@ WHERE city LIKE %@", tableName, city];
const char *sql = [sqlStr UTF8String];
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK) {
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(db));
}

sqlite3_finalize(statement);
sqlite3_close(db);

}

Solution

  • Try this statement

    NSString *sqlStr = [NSString stringWithFormat:@"DELETE FROM '%@' WHERE city LIKE '%@%%'", tableName, city];