我想记录表单数据并将其传递到另一页,所以我只想传递它(自动递增)行ID,然后在下一个函数中检索它。它正在正确创建数据库条目,但是游标lastrowid
始终在返回None
,所以我无法获取下一页的数据。
def connect_db():
"""Connects to the database."""
rv = sqlite3.connect(app.config['DATABASE'])
rv.row_factory = sqlite3.Row
return rv
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'sqlite_db'):
g.sqlite_db = connect_db()
return g.sqlite_db
@app.route('/choose', methods=['GET', 'POST'])
def input_values():
form = UserValuesForm()
if form.validate_on_submit():
g.db = get_db()
g.db.execute('insert into requests (occupants, '
'transmission_type, drive_type, engine_type, fuel_economy, '
'trunk_capacity, towing_capacity, safety_rating) '
'values (?, ?, ?, ?, ?, ?, ?, ?)',
[form.occupants.data, ';'.join(form.transmission_type.data),
';'.join(form.drive_type.data), ';'.join(form.engine_type.data),
form.fuel_economy.data, form.trunk_capacity.data,
form.towing_capacity.data, form.safety_rating.data])
g.last_req_id = g.db.cursor().lastrowid
g.db.commit()
return redirect('results/{0}'.format(str(g.last_req_id)))
return render_template('choose.html', form=form)
@app.route('/results/<int:req_id>', methods=['GET'])
def result(req_id):
return render_template('results.html')
另外,还有更好的方法吗?
您尝试从全新的游标中获取价值。您要使用从中获取值的同一光标执行插入。
cursor = g.db.cursor()
cursor.execute('...')
g.last_req_id = cursor.lastrowid
g.db.commit()
另外,您无需与之关联last_req_id
,g
因为您所做的全部是在中本地使用它input_values
。
last_req_id = cursor.lastrowid
return redirect('results/{0}'.format(last_req_id))
您还会看到我删除了对的呼叫str
。format
将为您处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句