Sometimes, we want to execute raw SQL in Flask-SQLAlchemy app with Python.
In this article, we’ll look at how to execute raw SQL in Flask-SQLAlchemy app with Python.
How to execute raw SQL in Flask-SQLAlchemy app with Python?
To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute
method.
For instance, we write
result = db.session.execute('SELECT * FROM my_table WHERE my_column = :val', {'val': 5})
to call db.session.execute
with the SQL string and dict with the values to fill for the placeholders.
We get the result returned from the select statement returned with execute
.
Conclusion
To execute raw SQL in Flask-SQLAlchemy app with Python, we can use the db.session.execute
method.