1

根据我的 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);

}
4

3 回答 3

1

检查 LIKE 的操作数是一个字符串(应该有引号)。我从未使用过 XCode,但如果您将第 5 行更改为:

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

那样有用吗?

于 2010-11-29T03:33:51.207 回答
1

试试这个说法

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

于 2010-11-29T03:57:29.897 回答
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);

}

我希望这个对你有帮助。

于 2010-11-29T03:54:59.720 回答