如何在 Flask-SQLAlchemy 应用程序中执行原始 SQL [英] How to execute raw SQL in Flask-SQLAlchemy app

查看:36
本文介绍了如何在 Flask-SQLAlchemy 应用程序中执行原始 SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SQLAlchemy 中执行原始 SQL?

How do you execute raw SQL in SQLAlchemy?

我有一个 python 网络应用程序,它运行在烧瓶上,并通过 SQLAlchemy 连接到数据库.

I have a python web app that runs on flask and interfaces to the database through SQLAlchemy.

我需要一种运行原始 SQL 的方法.查询涉及多个表连接以及内联视图.

I need a way to run the raw SQL. The query involves multiple table joins along with Inline views.

我试过了:

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

但我不断收到网关错误.

But I keep getting gateway errors.

推荐答案

您是否尝试过:

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() 是无连接",即 在 SQLAlchemy 2.0 中已弃用.

Note that db.engine.execute() is "connectionless", which is deprecated in SQLAlchemy 2.0.

这篇关于如何在 Flask-SQLAlchemy 应用程序中执行原始 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆