Python Flask PUT请求示例以更新SQLite DB

时间:2019-05-28 18:50:16

标签: python sqlite flask flask-restful

我正在用Python Flask写一个小的API,方法之一是PUT。我已经实现了GET,POST,DELETE,并且它们都按预期工作。我有一个SQLite数据库,需要使用PUT方法进行更新。有例子吗?

我的POST请求工作正常,我可以使用GET查看表中的新详细信息,但找不到正确的示例来更新表,例如“ UPDATE testtable set Name = Something,Sex = Something其中age = something“

class RecPost(Resource):        
    def post(self):
        conn = db_connect.connect()
        print(request.json)
        Name = request.json['Name']
        Sex = request.json['Sex']
        Age = request.json['Age']
        conn.execute("insert into testtable values('{0}','{1}','{2}')".format(Name, Sex, Age))
        return jsonify(request.json)

如果我使用与POST相同的构造,那么我会收到语法错误

class RecDel(Resource):        
    def put(self, Age):
        conn = db_connect.connect()
        print(request.json)
        Name = request.json['Name']
        Sex = request.json['Sex']
        Age = request.json['Age']
conn.execute("update testtable set Name=?, Sex=?, Age=? where Age =%d " %int(Age))

0 个答案:

没有答案