我对python、mysql和api开发还很陌生。所以,我创建了一个使用postman(在几个小时的在线研究之后)安排日历的简单请求。我可以在本地主机上创建一个mysql数据库。我可以使用python插入数据,但我想存储api响应,而不是手动存储。请看我的代码,如果有人能帮助我,我将非常感谢。
使用python的请求调用

    import requests

    url = "https://shedul.com/api/v1/appointments"
    querystring = {"minDate":"02/01/2019","maxDate":"02/08/2019"}

    payload = ""
    headers = {
        'Authorization': "Basic ************************"}

    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    print(response.json())

我会得到这样的响应(我会有很多请求和更多字段,但我需要这些字段)。
[
    {
        "id": 2649,
        "firstName": "Walikada",
        "lastName": "Sumana",
        "phone": "7894561452",
        "email": "wsumanar@gmail.com",
        "date": "February 8, 2019",
        "time": "2:00pm",
        "endTime": "3:00pm",
        "dateCreated": "February 5, 2019",
        "datetimeCreated": "2019-02-05T15:57:13-0600",
        "datetime": "2019-02-08T14:00:00-0700",
    },
    {
        "id": 264693950,
        "firstName": "Wade",
        "lastName": "Gulikapupusa",
        "phone": "789691985",
        "email": "W.Gulika77@yhaoo.com",
        "date": "February 8, 2019",
        "time": "2:00pm",
        "endTime": "2:20pm",
        "dateCreated": "February 4, 2019",
        "datetimeCreated": "2019-02-04T15:05:07-0600",
        "datetime": "2019-02-08T14:00:00-0600",
     }
]

我找到的连接mysql数据库的代码,
import mysql.connector

connection = mysql.connector.connect(
              host="localhost",
              user="root",
              passwd="admin",
              database="techbrandtest"
            )
mycursor = connection.cursor()

sqlcode = """INSERT INTO techbrandtest.acuity
                (
                    id,
                    firstName,
                    lastName,
                    phone,
                    email,
                    date,
                    time,
                    endTime,
                    dateCreated,
                    datetimeCreated,
                    datetime

                )
                VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""

insertvalues = ('546606', 'Suneth', 'Tharinda', '8016381256', 'wimalasena@gmail.com', '2019-02-02', '', '','2019-02-02','2019-02-02 23:59:59', '2019-02-05 23:59:59')

mycursor.execute(sqlcode, insertvalues)
connection.commit()
print(mycursor.rowcount, "was inserted.")

我想让它成为一个代码,并将每个响应存储在mysql数据库中。我不确定这是太多的要求,但任何帮助将是高度赞赏。
非常感谢你抽出时间

最佳答案

如果我正确地理解了您的问题,那么您应该能够通过简单地使用for循环来实现这一点。
例如。,

    for i in response:
        mycursor.execute(sqlcode, (i['id'],i['firstName']))

10-06 16:21
查看更多