如何在iOS应用程序中删除SQLite中的记录

时间:2013-06-12 15:51:58

标签: ios sqlite

我正在尝试删除我的应用程序中的记录,但我无法成功删除。

以下是代码段。

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

请告诉我可能出现的问题?

3 个答案:

答案 0 :(得分:2)

您的查询字符串似乎使用双引号而不是单引号:

NSString *querySQL = [NSString
    stringWithFormat: @"delete from TRACK_IT where TRACKING_NUMBER=\"%@\"",trkNum];
//                                                                  ^   ^
//                                                                  |   |
//                                                              Here and Here

用单引号替换应修复此语法错误:

NSString *querySQL = [NSString
    stringWithFormat: @"delete from TRACK_IT where TRACKING_NUMBER='%@'",trkNum];

但是,除非trkNum内置于您自己的程序中,否则请勿使用此方法删除记录:如果数据来自用户,则会将应用程序打开以进行SQL注入攻击

答案 1 :(得分:1)

ios swift中的简单删除操作

import UIKit
import SQLite3

let kCell = "Cell"

class ViewController: UIViewController {

    @IBOutlet weak var tfHeroName: UITextField!
    @IBOutlet weak var tfPowerRanking: UITextField!
    @IBOutlet weak var btnSave: UIButton!

    var db: OpaquePointer?
    private let refreshControl = UIRefreshControl()

    @IBOutlet weak var tableView: UITableView!

    var heroList = [Hero]()


    override func viewDidLoad() {
        super.viewDidLoad()

        //SetupPullTorefresh

        // Add Refresh Control to Table View
        if #available(iOS 10.0, *) {
            tableView.refreshControl = refreshControl
        } else {
            tableView.addSubview(refreshControl)
        }

        // Configure Refresh Control
        refreshControl.addTarget(self, action: #selector(refreshListData(_:)), for: .valueChanged)
        refreshControl.tintColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        refreshControl.attributedTitle = NSAttributedString(string: "Loading...")

        //database file address
        let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            .appendingPathComponent("HeroesDatabase.sqlite")

        //opening database
        if sqlite3_open(fileURL.path, &db) != SQLITE_OK {
            print("error opening database")
        }

        //creating table
        if sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS Heroes (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, powerrank INTEGER)", nil, nil, nil) != SQLITE_OK {
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("error creating table: \(errmsg)")
        }

        readValues()

    }

    @IBAction func tapOnBtnSave(_ sender: Any) {
        let name = tfHeroName.text?.trimmingCharacters(in: .whitespacesAndNewlines)
        let poweRanking = tfPowerRanking.text?.trimmingCharacters(in: .whitespacesAndNewlines)

        if name!.isEmpty {
            tfHeroName.layer.borderColor = #colorLiteral(red: 0.8058760762, green: 0.2736578584, blue: 0.1300437152, alpha: 1)
        }

        if poweRanking!.isEmpty {
            tfPowerRanking.layer.borderColor = #colorLiteral(red: 0.8058760762, green: 0.2736578584, blue: 0.1300437152, alpha: 1)
        }

        //creating a statement
        var stmt: OpaquePointer?

        //the insert query
        let queryString = "INSERT INTO Heroes (name, powerrank) VALUES (?,?)"

        //preparing the query
        if sqlite3_prepare(db, queryString, -1, &stmt, nil)  != SQLITE_OK {

            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("error preparing insert: \(errmsg)")
            return

        }

        //binding the parameters

        if sqlite3_bind_text(stmt, 1, name, -1, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")
            return
        }

        if sqlite3_bind_int(stmt, 2, (poweRanking! as NSString).intValue) != SQLITE_OK {
            let errmsg = String(cString: sqlite3_errmsg(db))
            print("failure binding name: \(errmsg)")
        }

        //executing the query to insert values
        if sqlite3_step(stmt) != SQLITE_DONE {
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("failure binding name: \(errmsg)")

        }

        //emptying the textfields
        tfHeroName.text = ""
        tfPowerRanking.text = ""


    }

    //MARK: Read Operation
    func readValues(){
        heroList.removeAll()

        let queryString = "SELECT * FROM Heroes"

        var stmt:OpaquePointer?

        if sqlite3_prepare(db, queryString, -1, &stmt, nil) != SQLITE_OK{
            let errmsg = String(cString: sqlite3_errmsg(db)!)
            print("error preparing insert: \(errmsg)")
            return
        }

        while(sqlite3_step(stmt) == SQLITE_ROW){
            let id = sqlite3_column_int(stmt, 0)
            let name = String(cString: sqlite3_column_text(stmt, 1))
            let powerrank = sqlite3_column_int(stmt, 2)

            heroList.append(Hero(id: Int(id), name: String(describing: name), powerRanking: Int(powerrank)))
        }

        self.tableView.reloadData()
        self.refreshControl.endRefreshing()
    }

    //MARK: Delete Operation
    func deleteItemFromList(itemId: Int32){

        let deleteStatementStirng = "DELETE FROM Heroes WHERE id = ?;"
        var deleteStatement: OpaquePointer? = nil

        if sqlite3_prepare(db, deleteStatementStirng, -1, &deleteStatement, nil) == SQLITE_OK {

            sqlite3_bind_int(deleteStatement, 1, itemId)

            if sqlite3_step(deleteStatement) == SQLITE_DONE {
                print("Successfully deleted row.")
            } else {
                print("Could not delete row.")
            }
        } else {
            print("DELETE statement could not be prepared")
        }

        sqlite3_finalize(deleteStatement)
        print("delete")
    }


    @objc private func refreshListData(_ sender: Any) {
        readValues()
    }

}

extension ViewController : UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return heroList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
        let hero: Hero
        hero = heroList[indexPath.row]
        cell.textLabel?.text = hero.name
        return cell
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == UITableViewCell.EditingStyle.delete {
            heroList.remove(at: indexPath.row)
            deleteItemFromList(itemId: Int32(heroList[indexPath.row].powerRanking))
            tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
            tableView.reloadData()
        }

    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

}


class Hero {

    var id: Int
    var name: String?
    var powerRanking: Int

    init(id: Int, name: String?, powerRanking: Int){
        self.id = id
        self.name = name
        self.powerRanking = powerRanking
    }
}

答案 2 :(得分:0)

从数据库中删除

- (IBAction)删除:(UIButton *)发件人{

sqlite3_stmt *statement;
const char *dbPath = [databasePath UTF8String];

if(sqlite3_open(dbPath, &contactDB)== SQLITE_OK){

    NSString *deleteSQL = [NSString stringWithFormat:@"DELETE FROM CONTACTS WHERE NAME = \"%@\"",_name.text];

    const char *delete_stmt = [deleteSQL UTF8String];
    sqlite3_prepare_v2(contactDB, delete_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement)== SQLITE_DONE) {
        _status.text = @"Contact removed";
        _name.text = @"";
        _address.text = @"";
        _phoneNo.text = @"";

    }else{
        _status.text = @"Failed to delete";
    }
    sqlite3_finalize(statement);
    sqlite3_close(contactDB);

}

}