python - 如何在 Flask-SQLAlchemy 应用程序中执行原始 SQL

如何在 SQLAlchemy 中执行原始 SQL?

我有一个 Python Web 应用程序,它在 flask 上运行并通过 SQLAlchemy 连接到数据库。

我需要一种方法来运行原始 SQL。该查询涉及多个表连接以及内联 View 。

我试过了:

connection = db.session.connection()
connection.execute( <sql here> )

但我不断收到网关错误。

最佳答案

你试过了吗:

result = db.engine.execute("<sql here>")

或:

from sqlalchemy import text

sql = text('select name from penguins')
result = db.engine.execute(sql)
names = [row[0] for row in result]
print names

请注意,db.engine.execute() 是“无连接”,即 deprecated in SQLAlchemy 2.0 .

https://stackoverflow.com/questions/17972020/

相关文章:

python - 输入()错误 - NameError : name '...' is not de

linux - 如何获取进程 ID 以杀死 nohup 进程?

linux - Bash 脚本并行处理有限数量的命令

linux - 如何将命令行参数传递给 unix/linux 系统上正在运行的进程?

python - 如何从文件中读取特定行(按行号)?

python - Pandas :从多级列索引中删除一个级别?

python - Google 大量使用 Python

linux - 第一次如何配置postgresql?

python - 在python中将 float 转换为整数的最安全方法?

linux - 如何规范化 Bash 中的文件路径?