title: How do I connect iOS app with SQL database?
body: <p>I am trying to create an iOS application that sends JSON data to an SQLAlchemy database using flask. I'm fairly new to this, so I'm probably making some obvious mistakes. I'm getting an error on the swift side when I run the application saying</p>
  <p><strong>&quot;Could not connect to server&quot;</strong></p>
  <p>This is my Swift code:</p>
  <pre><code>enum APIError: Error {
  case responseProblem
  case decodingProblem
  case encodingProblem } 
  
  struct APIRequest {
  let resourceURL: URL
  
  init() {
      let resourceString =  &quot;http://127.0.0.1:5000/json-example&quot;
      guard let resourceURL = URL(string: resourceString) else {fatalError()}
      
      self.resourceURL = resourceURL
  }
  
  func save (_ dataToSave:String, completion: @escaping(Result&lt;Any, APIError&gt;) -&gt; Void) {
      
      do{
          var urlRequest = URLRequest(url: resourceURL)
          urlRequest.httpMethod = &quot;POST&quot;
          urlRequest.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
          urlRequest.httpBody = try JSONEncoder().encode(dataToSave)
          
          let dataTask = URLSession.shared.dataTask(with: urlRequest) { data, response, _ in
              guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200, let jsonData = data else {
                  completion(.failure(.responseProblem))
                  return
              }
              do {
                  let GPSData = try JSONDecoder().decode(String.self, from: jsonData)
                  completion(.success(GPSData))
              }catch{
                  completion(.failure(.decodingProblem))
              }
              
          }
          dataTask.resume()
      }catch{
          completion(.failure(.encodingProblem))
      }
  }
  }
  </code></pre>
  <p>And this is my Python code for the database:</p>
  <pre><code>from flask import Flask, request
  from flask_sqlalchemy import SQLAlchemy
  
  app = Flask(__name__)
  
  app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = 'sqlite:///data.db'
  
  db = SQLAlchemy(app)
  
  @app.route(&quot;/&quot;)
  def home():
      return &quot;Connection success&quot;
  
  @app.route('/json-example', methods=['POST'])
  def json_example():
      request_data = request.get_json()
  
      lattitude = None
      longitude = None
  
  if request_data:
      if 'lattitude' in request_data:
          lattitude = request_data['lattitude']
  
      if 'longitude' in request_data:
          longitude = request_data['longitude']
  
  
  return &quot;connected&quot;
         
         
  class GPS(db.Model):
      __tablename__ = 'GPS Coordinates'
  
      id = db.Column(db.Integer(), primary_key=True)
      longitude = db.Column(db.Float)
      latitude = db.Column(db.Float)
  
  def __repr__(self):
      return f&quot;GPS('{self.longitude}', '{self.latitude}')&quot;
  
  
  if __name__ == '__main__':
      app.run(debug=True, port=5000)
  </code></pre>
  <p>Any help will be much appreciated!</p>