iOS插入数据库

时间:2018-02-06 11:21:21

标签: ios swift

朋友们,我只是想用api添加,更新和删除数据。但每当我点击添加数据时,我需要存储id以进行删除和更新。有人可以帮助我在添加数据库时如何存储id,我可以将id传递给下一个视图控制器。请让我知道只是存储ID,同时添加数据库

import UIKit
import Alamofire

class ViewController: UIViewController {
    var data = NSArray()
    var datasnew = NSArray()
    @IBOutlet var lastnameText: UITextField!

    @IBOutlet var citytext: UITextField!
    @IBOutlet var firstnametext: UITextField!

    @IBAction func add(_ sender: Any) {
        if firstnametext.text == ""{
            print("please enter some text")
        } else {
            let vc = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
            vc.first = firstnametext.text!
            vc.last = lastnameText.text!
            vc.city = citytext.text!
            vc.id = ""

            let parameters: Parameters = [
                "fname":"\(firstnametext.text!)",
                "lname":"\(lastnameText.text!)",
                "city":"\(citytext.text!)"
            ]

            Alamofire.request("http://jewelish.co.in/test/insert.php", method: .post, parameters: parameters).responseJSON { (response) in
                if let JSON = response.result.value{
                    //   print("JSON: \(JSON)")

                    let dict = JSON as! NSDictionary
                    //  print("dict: \(dict)")
                }

                self.present(vc, animated: true, completion: nil)
            }
            self.data.addingObjects(from: [datasnew])
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request("http://jewelish.co.in/test/get.php", method: .post).responseJSON { (response) in
            if let JSON = response.result.value{
                //   print("JSON: \(JSON)")

                let dict = JSON as! NSDictionary
                //  print(dict)
                self.data = dict.value(forKey: "Data")! as! NSArray

                let fname = self.data.value(forKey: "fname")
                //  print(fname)
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

据我了解,您正在等待导航到下一个屏幕,直到您收到服务器的响应。因此,在调用API http://jewelish.co.in/test/insert.php后,您可能会收到json或xml的响应,例如,您正在获取

    { fnale:"first name", 
      lname: "last name", 
      . 
      . 
      ., 
      id={generated from backend developer} 
    }

因此,请尝试在响应中再添加一个键作为 id ,这将从后端开发人员生成。并更新您的@IBAction func add(_ sender: Any) {}功能,如下所示。

@IBAction func add(_ sender: Any) {
    if firstnametext.text == ""{
        print("please enter some text")
    } else {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
        vc.first = firstnametext.text!
        vc.last = lastnameText.text!
        vc.city = citytext.text!
        vc.id = ""

        let parameters: Parameters = [
            "fname":"\(firstnametext.text!)",
            "lname":"\(lastnameText.text!)",
            "city":"\(citytext.text!)"
        ]

        Alamofire.request("http://jewelish.co.in/test/insert.php", method: .post, parameters: parameters).responseJSON { (response) in
            if let JSON = response.result.value{
                //   print("JSON: \(JSON)")

                let dict = JSON as! NSDictionary
                vc.id = dict["id"]
                //  print("dict: \(dict)")
            }

            self.present(vc, animated: true, completion: nil)
        }
        self.data.addingObjects(from: [datasnew])
    }

}

注意: 我已经检查了您的API网址,我可以看到以下内容作为回复,因此请您的API开发人员按照上面的说明为您提供修改后的响应。

{
status: 1,
message: "Successfully done"
}