How to execute raw SQL in SQLAlchemy-flask app

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

From: stackoverflow.com/q/17972020

Back to homepage or read more recommendations: