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

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

问题描述



我有一个python web应用程序,它通过SQLAlchemy运行在数据库和接口上。

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



我试过了:

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

网关错误。

解决方案

您是否尝试过:

 


$ p >或者:

  from sqlalchemy import text 

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

打印名称


How do you execute raw SQL in SQLAlchemy?

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

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

I've tried:

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

But I keep getting gateway errors.

解决方案

Have you tried:

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

or:

from sqlalchemy import text

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

print names

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

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