Sqlalchemy - executing raw sql queries

こ雲淡風輕ζ 提交于 2019-12-19 11:28:17

问题


I'm using sqlalchemy in a flask application that connects to multiple databases, using binds as shown here. I want to execute a raw sql query on one of the non-primary databases.

I'm trying to use session.execute, as shown here but it executes for the primary db. The API docs state that you can use a parameter: "bind – Optional Engine to be used as the bind". How do I access and specify the bind for the non-primary db and call session.execute to execute a query for that db?

Alternately, is there any other way to go about it?


回答1:


Try this:

from flask import current_app
db.get_engine(current_app, bind='<your_bind>').execute('<your raw sql>')


来源:https://stackoverflow.com/questions/39664898/sqlalchemy-executing-raw-sql-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!