我正在使用Falcon和SQlite3制作我的第一个RESTful应用程序接口。我有一个名为students的数据库和表。每个学生都有姓名和年龄属性。在students表中有几个学生。
我想使用我创建的API将一个新学生插入到我的表中。我正在使用Insomnia发出请求。
这是我的请求的样子,它的JSON对象:
{
"name":"JACK",
"age":"213"
}Leater我将JSON中的值赋给变量,然后将这些名称放入查询中。
我的API:
import falcon
import json
from sqlalchemy import create_engine
engine = create_engine('sqlite:////home/konrad/Desktop/source/falcon/database.db')
class testAPI(object):
def on_post(self, req, resp):
connection = engine.connect()
data = json.loads(req.stream.read())
ageV = data['age']
nameV = data['name']
//This is how I am trying to insert data.
//connection.execute("INSERT INTO students (name, age) VALUES (nameV, ageV)")
connection.close()
print(data['name'])
resp.body = data['name']
resp.status = falcon.HTTP_200
app = falcon.API()
xd = testAPI()
app.add_route('/', xd)这种方式行不通。我应该如何将值插入到表中?
发布于 2018-02-28 05:28:30
我已经解决了我的问题。它是关于python语法的。
connection.execute("INSERT INTO students (name, age) VALUES (?, ?)", (nameV, ageV))https://stackoverflow.com/questions/49015969
复制相似问题