使用以下代码无法更新记录

时间:2016-07-27 13:44:48

标签: ios swift sqlite

的问题:

我无法在项目中使用以下代码更新插入的记录。如果单击更新按钮,则应该更新记录,并且应该在Viewcontroller中显示

代码:

import UIKit

class UpdateScreen: UIViewController
{

    var strEmail:String!
    var dictRecord:NSDictionary!
    var databasepath:String!

    @IBOutlet weak var txtfld_Update_Name: UITextField!

    @IBOutlet weak var txtfld_Update_Phone: UITextField!

    @IBOutlet weak var txtfld_Update_Email: UITextField!

    @IBOutlet weak var txtfld_Update_Comment: UITextField!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        print(dictRecord!)

        txtfld_Update_Name.text! = (dictRecord["Name"]! as? String)!
        txtfld_Update_Phone.text! = (dictRecord["Phone"]! as? String)!
        txtfld_Update_Email.text! = (dictRecord["Email"]! as? String)!
        txtfld_Update_Comment.text! = (dictRecord["Comment"]! as? String)!

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    @IBAction func btnUpdateClicked(sender: AnyObject)
    {
        let filemgr = NSFileManager.defaultManager()
        let dirpaths = NSSearchPathForDirectoriesInDomains(.DocumentationDirectory, .UserDomainMask, true)
        let docDir = dirpaths[0] as String
        databasepath = docDir.stringByAppendingString("StudentDataManipulation.sqlite");

        if filemgr.fileExistsAtPath(databasepath as String)
        {
            let contactDB = FMDatabase(path: databasepath as String)

            if contactDB.open()
            {
                let sql_stmt = "CREATE TABLE IF NOT EXISTS StudentData (Name TEXT, Phone TEXT, Email TEXT PRIMARY KEY  NOT NULL, Comment TEXT)"

                if !contactDB.executeStatements(sql_stmt)
                {
                    print("Error: \(contactDB.lastErrorMessage())")
                }


                let updateSql:String = "UPDATE StudentData SET Name='\(txtfld_Update_Name.text!)', Phone='\(txtfld_Update_Phone.text!)', Comment='\(txtfld_Update_Comment.text!)'"

                print("updateSql before = \(updateSql)");

                let result = contactDB.executeUpdate(updateSql, withArgumentsInArray: nil)

                print("updateSql after = \(updateSql)");

                if !result
                {
                    print("Error: \(contactDB.lastErrorMessage())")
                }
                else
                {
                    print("Record Successfully Updated")
                    self.navigationController?.popViewControllerAnimated(true)
                }
            }
            else
            {
                print("Error: \(contactDB.lastErrorMessage())")
            }
            contactDB.close()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的sql错了。 update tablename set field1 = value1 and field2 = value2 where id = xx