无法从SQLite数据库中删除行,但不会发出错误

时间:2010-11-29 03:24:21

标签: iphone sqlite delete-row

根据我的Xcode调试器,我使用的是从项目中的表中删除行的正确语法。但是,当我回去检查我的数据库时,它仍然存在。我用于插入条目的其他SQL语法是正确的,所以我不确定我做错了什么。 NSLogs确认两个变量都正确发送:

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

}

3 个答案:

答案 0 :(得分:1)

检查LIKE的操作数是否为字符串(应该在其周围加上引号)。我从来没有使用过XCode,但是如果你把第5行更改为:

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

这有用吗?

答案 1 :(得分:1)

试试这句话

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

答案 2 :(得分:0)

你刚刚准备好了声明,你应该执行该声明。

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

}

我希望这个会帮助你。