HTTP更新请求 - 如何仅更新特定字段?

时间:2017-10-10 15:35:18

标签: python curl flask

我在Flask和Python中写作。我的更新方法如下所示:

def post(self, medicine_id):
        conn = db_connect.connect()
        last_name = request.json.get('LastName')
        first_name = request.json.get('FirstName')
        title = request.json.get('Title')
        reports_to = request.json.get('ReportsTo')
        birth_date = request.json.get('BirthDate')
        hire_date = request.json.get('HireDate')
        address = request.json.get('Address')
        city = request.json.get('City')
        state = request.json.get('State')
        country = request.json.get('Country')
        postal_code = request.json.get('PostalCode')
        phone = request.json.get('Phone')
        fax = request.json.get('Fax')
        email = request.json.get('Email')
        conn.execute ("UPDATE employees SET LastName='{0}',FirstName='{1}',Title='{2}', \
                      ReportsTo='{3}',BirthDate='{4}',HireDate='{5}',Address='{6}',City='{7}', \
                      State='{8}',Country='{9}',PostalCode='{10}',Phone='{11}',Fax='{12}', \
                      Email='{13}' WHERE EmployeeId={14}".format(last_name, first_name, title,
                                                                 reports_to, birth_date, hire_date,
                                                                 address, city, state, country,
                                                                 postal_code, phone, fax,
                                                                 email, medicine_id))
        return jsonify({"status": "success"})

我需要更新数据库中的特定行。我用于那个看起来像这样的HTTP请求:

curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d "{\"City\":\"Lodz\", \"FirstName\":\"Patryk\"}" http://127.0.0.1:8000/edit/126

在这种情况下,我更新ID等于126的行。它会更新" City"和" FirstName"列,但它也改变其他列与"无"。我的问题是如何修改我的curl请求或Python代码只是为了更新curl请求中指定的列。感谢

0 个答案:

没有答案