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

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

问题描述

如何在SQLAlchemy中执行原始SQL?

How do you execute raw SQL in SQLAlchemy?

我有一个运行在flask上并通过SQLAlchemy连接到数据库的python Web应用程序.

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

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

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