我正在创建一个搜索工具,该工具使用psycopg,flask和SQLAlchemy从Postgres数据库返回结果。我收到错误消息:sqlalchemy.engine.result.ResultProxy对象位于...当我搜索“职位编号”时,就会发生这种情况。我被认为这是由于未与ResultProxy接口,但我无法弄清楚哪里出了问题?app.pyfrom flask import Flask, render_template, requestfrom sqlalchemy import create_enginefrom sqlalchemy import textfrom flask_sqlalchemy import SQLAlchemyapp = 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'] = Falsedb=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)Main.html<!DOCTYPE html><html><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>任何帮助将不胜感激。
1 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
也许您应该使用sqlalchemy
来执行 rp = db.session.execute("select * from test")
result_set = rp.fetchall()
添加回答
举报
0/150
提交
取消