我正在移植我使用 mysql 库来查询 mysql 的旧代码。我试图使用flask_SQLALchemy 来做同样的事情,但无法找到查询“或”的替代选项。
下面是更新后的代码片段,介绍了它的工作原理。
from flask_sqlalchemy import SQLAlchemy
from myapp import APP
from myapp.models.nodes import Host
DB = SQLAlchemy(APP)
# config is taken care here
APP.app_context().push()
with APP.app_context():
DB.init_app(APP)
Hostname_var = "some hostname"
session_query = Host.query.filter_by(hostname=hostname_var).first()
Run Code Online (Sandbox Code Playgroud)
同样,我想查询下面给出的语句。我可以使用 sqlalchemy 但使用不同的库听起来并不理想。
"Select count(*) as c from table_name where ver='some version' and arch='some arch' and pool=1 and (state='Ready' or state='Provisioning');"
table_name 的类称为 Host(如示例中所使用的)。
如有任何帮助,我们将不胜感激,谢谢。