【发布时间】:2021-12-12 07:41:55
【问题描述】:
我想使用单个 JSON 输入将多个条目放入表中,但我不知道如何从这里开始。一旦用户声明,我就会到达那里
[
{
"VIN": "kjasdfh",
"Make": "Toyota",
"model": "Corolla",
"Year": 1998
},
{
"VIN": "wqeiryu",
"Make": "Honda",
"model": "Civic",
"Year": 1997
}
]
我不会让第一个有自己的条目,而第二个有另一个条目。
@app.route('/api/addcar', methods = ['POST']) # This is a post method because the user needs to be able to add info
def adding_stuff():
request_data = request.get_json() # Gets the info from the table and converts to JSON format
new_vin = request_data['VIN']
new_make = request_data['Make']
new_year = request_data['Year']
new_color = request_data['Color']
sql = "INSERT INTO carsTEST (VIN, Make, Year, Color, username) VALUES ('%s', '%s', %s, '%s')" % (new_vin, new_make, new_year, new_color) # This sql statement will then be uploaded to the databse to add a new record
conn = create_connection()
execute_query(conn, sql) # This will execute the query
return 'Post worked'
【问题讨论】:
-
不太确定您需要什么帮助。您绝对应该取出
'%s'位并替换为?。也许这会有所帮助:pythonbasics.org/flask-sqlite