如何在 iOS 应用程序中删除 SQLite 中的记录 [英] How to delete records in SQLite in iOS Application
本文介绍了如何在 iOS 应用程序中删除 SQLite 中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除应用程序中的记录,但未能成功.
I am trying t delete records in my application, but I could not successfully able to do so.
这是代码片段.
if(trackingHistory != nil){
for (NSString *trkNum in trackingHistory) {
if(trkNum == NULL){
continue;
}
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: @"delete from TRACK_IT where TRACKING_NUMBER=\"%@\"",trkNum];
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(database, query_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"deleted record");
//return YES;
} else {
NSLog(@"Failed to delete record");
// return NO;
}
sqlite3_reset(statement);
}
}
}
请告诉我可能有什么问题?
Please let me know what could be wrong?
推荐答案
您的查询字符串似乎使用双引号而不是单引号:
Your query string appears to be using double quotes instead of single quotes:
NSString *querySQL = [NSString
stringWithFormat: @"delete from TRACK_IT where TRACKING_NUMBER=\"%@\"",trkNum];
// ^ ^
// | |
// Here and Here
用单引号替换应该可以解决这个语法错误:
Replacing with single quotes should fix this syntax error:
NSString *querySQL = [NSString
stringWithFormat: @"delete from TRACK_IT where TRACKING_NUMBER='%@'",trkNum];
但是,除非 trkNum
内置在您自己的程序中,否则不要使用这种删除记录的方法:如果数据来自用户,您的应用程序就会受到 SQL 注入攻击.
However, unless trkNum
is built into your own program, do not use this method of deleting records: if the data comes from the user, you open your app to SQL injection attacks.
这篇关于如何在 iOS 应用程序中删除 SQLite 中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文