首页 > 解决方案 > 如何通过python脚本将信息从mysql数据库发送到ios应用程序

问题描述

我想使用 python 脚本和烧瓶服务器将信息从 mysql 数据库发送到 ios 应用程序。我目前可以通过以下脚本从应用程序更新数据库。

from flask import Flask
from flask import make_response
from flask import request
import mysql.connector

#opens database connectionto "events" database
try:
    mydb = mysql.connector.connect(host="localhost", user="root", passwd="Pass", database = "events")
    print("Connection OK")

except e:
    print("Connection failed: ", e)
#prepare a cursor object using cursor() method

mycursor = mydb.cursor()

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def register():

    if flask.request.method == 'POST':
        event_id = request.form['a']
        poi = request.form['b']
        address = request.form['c']
        start_time = request.form['d']
        end_time = request.form['e']

        sql = "INSERT INTO events (event_id, poi, address, start_time, end_time) VALUES (%s, %s, %s, %s, %s)"
        val = (event_id, poi, address, start_time, end_time)


        try:
           # Execute the SQL command
           mycursor.execute(sql, val)
           # Commit your changes in the database
           mydb.commit()
           mydb.close()
        except e:
           # Rollback in case there is any error
           print("Error: ", e)
           mydb.rollback()

        return make_response("Success!", 200)

    elif flask.request.method == 'GET':


           return make_response("Success!", 200)

print("DB is closed")

我不确定如何在我的脚本和我的应用程序中实现 get 方法以从数据库中获取信息。我想提供信息的 swift 文件看起来像这样

class AnnotationsVM: ObservableObject {
    @Published var annos = [MGLPointAnnotation]()
    @Published var annotationPlacementFailed: Bool
    @ObservedObject var VModel = ViewModel()
    @Published var visited: Bool


//Need to change this so the first annotation that is created is the user's annotation. Alternatively, we could just not have a user annotation and just use the standard blue circle as user location for now.

    init() {
        let annotation = MGLPointAnnotation()
        annotation.title = "Shoe Store"
        annotation.coordinate = CLLocationCoordinate2D(latitude: 40.78, longitude: -73.98)
        annotation.subtitle = "Shoe Different"
        annotationPlacementFailed = false
        visited = true
        annos.append(annotation)


    }
}

我想使用数据库中的信息在 init 函数中创建注释,但不知道如何在 swift 中创建 get 函数。

简而言之,我需要帮助在我的烧瓶 python 脚本和我的 ios 应用程序的 swift 文件中编写数据库获取方法

标签: pythoniosmysqlswiftflask

解决方案


推荐阅读