title: SQLite Swift Binding and Retrieving
body: <p>I am having an issue with either the binding and inserting of data or the retrieval of my data. I'm honestly not sure how to determine which is causing the issue. I am trying to add a <code>textfield</code>, <code>latitude</code>, and <code>longitude</code> to the database using the code below. I am almost positive the problem lies with the <code>sqlite3_bind_text</code> or the <code>sqlite3_column_text</code> functions but I am including both the functions in their entirety for context. When I run my debug print statements every value is printed out as the longitude value. Almost as if the longitude value is overwriting the other two columns? Is the way I am passing <code>latitude</code> and <code>longitude</code> to bind incorrectly? Or am I retrieving the data incorrectly?</p>
  
  <pre><code>@IBAction func submitPressed(_sender: UIButton) {
  
      var lat = ""
      var long = ""
  
      if(isLocationEnabled.isOn == true){
          enableLocationManager()
          let location:CLLocationCoordinate2D = locationManager.location!.coordinate
          lat = String(location.latitude)
          long = String(location.longitude)
          print(lat)
          print(long)
  
      } else {
          disableLocationManager()
          lat = "44.5"
          long = "-123.2"
      }
  
      let text = textEntered.text?.trimmingCharacters(in: .whitespacesAndNewlines)
  
  
      if(text?.isEmpty)!{
          textEntered.layer.borderColor = UIColor.red.cgColor
      }
  
      var statement: OpaquePointer?
  
      let newQuery = "INSERT INTO Location (textEntered, latitude, longitude) VALUES (?,?,?)"
  
      if sqlite3_prepare(database, newQuery, -1, &amp;statement, nil) != SQLITE_OK {
          let error = String(cString: sqlite3_errmsg(database)!)
          print("Error Preparing: \(error)")
          return
      }
  
      if sqlite3_bind_text(statement, 1, text, -1, nil) != SQLITE_OK {
          let error = String(cString: sqlite3_errmsg(database)!)
          print("Error Binding: \(error)")
          return
      }
  
      if sqlite3_bind_text(statement, 2, lat, -1, nil) != SQLITE_OK {
          let error = String(cString: sqlite3_errmsg(database)!)
          print("Error Binding: \(error)")
          return
      }
  
      if sqlite3_bind_text(statement, 3, long, -1, nil) != SQLITE_OK {
          let error = String(cString: sqlite3_errmsg(database)!)
          print("Error Binding: \(error)")
          return
      }
  
      if sqlite3_step(statement) != SQLITE_DONE {
          let error = String(cString: sqlite3_errmsg(database)!)
          print("Error Inserting: \(error)")
          return
      }
  
      textEntered.text = ""
  </code></pre>
  
  <p>Then, I try to retrieve it using the code below:</p>
  
  <pre><code>func readValues() {
      posts.removeAll()
  
      let newQuery = "SELECT id, textEntered, latitude, longitude FROM Location"
  
      var statement: OpaquePointer?
  
      if sqlite3_prepare(database, newQuery, -1, &amp;statement, nil) != SQLITE_OK {
          let error = String(cString: sqlite3_errmsg(database)!)
          print(error)
      }
  
      while(sqlite3_step(statement) == SQLITE_ROW) {
          let id = sqlite3_column_int(statement,0)
          let textEntered = String(cString: sqlite3_column_text(statement,1))
          let latitude = String(cString: sqlite3_column_text(statement,2))
          let longitude = String(cString: sqlite3_column_text(statement,3))
  
  
          print("Text Entered: \(textEntered)")
          print("Latitude: \(latitude)")
          print("Longitude: \(longitude)")
  
          posts.append(Post(id: Int(id),textEntered: String(describing: textEntered), latitude: String(describing: latitude), longitude: String(describing: longitude)))
      }
  
      self.tableViewPosts.reloadData()
  
  }
  </code></pre>
  
  <p>EDIT: I was able to determine that there is a flaw in the insert. When I view the SQLite database through DB Browser I see that the longitude value is being inserted into all of the columns. I'm just not sure where my syntax is flawed in the sqlite3_bind_text.
  <a href="https://i.stack.imgur.com/1yuwz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1yuwz.png" alt="enter image description here"></a></p>