开发者

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:(N开发者_如何学PythonSString *)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);

}


Try this statement

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


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜