gpt4 book ai didi

python - 从插入查询中获取数据?

转载 作者:行者123 更新时间:2023-11-30 21:29:15 24 4
gpt4 key购买 nike

我在这里为一个食谱创建了一个帖子,其中有一个 recipe_id并自动递增,在同一页面上,它将返回要添加到食谱中的配料列表

def recipeCreate():                                                                                                                           

if request.method == 'POST':
userposted = session['user_ID']
title = request.form['title']
prep_time = request.form['preptime']
cook_time = request.form['cooktime']
description = request.form['description']
category = request.form['category']
diet = request.form['diet']
calories = request.form['calories']

c = sq.connection.cursor()
c.execute('INSERT INTO recipe_post (posted_by, name, description, prep_time, cook_time, category, diet, calories)VALUES(%s,%s,%s,%s,%s,%s,%s,%s)',(userposted, title, description, prep_time, cook_time, category, diet, calories))
sq.connection.commit()
c.close()

return "<h1>Form successfully submitted</h1>"

c = sq.connection.cursor()
result = c.execute('SELECT * FROM Ingredients')
data = c.fetchall()

return render_template('recipe/recipe_form.html', data=data)

配料表上的每种配料都是一个 <a>将链接到此的标签将为帖子添加成分

@app.route('/recipe/create/add/<recipe_id>/<ingredient_id>/<quantity>')                                                               
def addRecipeIngredient(recipe_id,ingredient_id,quantity):
recipe_id = int(recipe_id)
ingredient_id = int(ingredient_id)
quantity = int(quantity)
c = sq.connection.cursor()

result = c.execute('INSERT INTO recipe_ingredients(recipe_id,i_id,quantity) VALUES(%s,%s,%s)',(recipe_id,ingredient_id,quantity))
sq.connection.commit()
c.close()

INSERT INTO recipe_ingredients(recipe_id,i_id,quantity) VALUES(%s,%s,%s)

我的问题是我无法获得 recipe_id来自 recipeCreate() , 插入到配方成分表中,因为它是尚未创建/查询的帖子。

我正在考虑插入 recipe_post数据,然后在下一页插入成分数据,但我仍然遇到获取 recipe_id 的问题.

我该如何解决这个问题,有没有办法从插入查询中获取数据?

最佳答案

您可以使用 sq.connection.insert_id()

请求连接上的最后一个 ID

如果该功能不可用,请告诉我您正在使用的 sql 库的名称。

关于python - 从插入查询中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57206930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com