SQLAlchemy引擎结果代理错误(Python)
我正在创建一个搜索工具,它使用psycopg、flask和SQLAlchemy从Postgres数据库返回结果 我得到了一个错误: sqlalchemy.engine.result.ResultProxy对象位于 这是在我搜索“工作编号”时发生的 我被引导相信这是由于没有与ResultProxy接口,但我无法找出哪里是错的 app.py Main.htmlSQLAlchemy引擎结果代理错误(Python),python,flask,sqlalchemy,psycopg,Python,Flask,Sqlalchemy,Psycopg,我正在创建一个搜索工具,它使用psycopg、flask和SQLAlchemy从Postgres数据库返回结果 我得到了一个错误: sqlalchemy.engine.result.ResultProxy对象位于 这是在我搜索“工作编号”时发生的 我被引导相信这是由于没有与ResultProxy接口,但我无法找出哪里是错的 app.py Main.html xx x {{result_set}} 非常感谢您的帮助。也许您应该使用sqlalchemy来执行 rp=db.session.exe
xx
x
{{result_set}}
非常感谢您的帮助。也许您应该使用
sqlalchemy
来执行
rp=db.session.execute(“从测试中选择*)
result_set=rp.fetchall()
为什么在实际获取结果之前要关闭它?请发布完整的回溯。@keyring88我的荣幸,请接受答案并关闭此问题
from flask import Flask, render_template, request
from sqlalchemy import create_engine
from sqlalchemy import text
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://xx:xx@rx:x/xxx'
engine = create_engine('postgresql+psycopg2://xx:xxx@rx:x/x')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db=SQLAlchemy(app)
@app.route('/', methods=['GET', 'POST'])
def homepage():
if request.method == 'POST':
jn = request.form['jobnumber']
result_set = engine.execute(text("SELECT cost FROM public.options where cast(optionno AS VARCHAR) LIKE :jn"), {"jn": f"%{jn}%"})
result_set.close()
return render_template('main.html', result_set=result_set, jn=jn)
else:
return render_template('main.html')
if __name__ == "__main__":
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>xx</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
</head>
<body>
<p>x</p>
<form method="POST" id="jobnumber">
<input name="jobnumber" type="textbox" placeholder="jobnumber">
</form>
<table>
<td>
{{result_set}}
</td>
</table>
</body>
</html>