squeezemylime
squeezemylime

Reputation: 2102

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

}

Upvotes: 1

Views: 3282

Answers (3)

Swapna
Swapna

Reputation: 2235

Try this statement

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

Upvotes: 1

KingofHeaven
KingofHeaven

Reputation: 1205

you have just prepared the statement, you should step that statement.

-(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) 
{
      /**this one will execute when there is error in your query**/
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(db));
}
else
{
     /************just add this one****************/
     sqlite3_step(statement);
}

sqlite3_finalize(statement);
sqlite3_close(db);

}

i hope this one will help you.

Upvotes: 0

jon
jon

Reputation: 11

Check that the operand to LIKE is a string (should have quotation marks around it). I've never used XCode, but if you change your 5th line to read:

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

Does that work?

Upvotes: 1

Related Questions