Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cursor in SQLAlchamy

I am newbie in Python Flask. In my project we are creating db object using below code.

    app = Flask(__name__)  
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'  
    db = SQLAlchemy(app)   

I want to get cursor object from db. Can someone please help me on it. I know using connection object we can get cursor. But can we get cursor from db object which is created by above way? Thanks.

like image 461
user3462649 Avatar asked Apr 12 '18 13:04

user3462649


2 Answers

Finally got answer from Flask documentation, we can get cursor from db object using,

from sqlalchemy import create_engine
engine = create_engine('your_connection_string')
connection = engine.raw_connection()
cursor = connection.cursor()
like image 117
user3462649 Avatar answered Sep 20 '22 17:09

user3462649


It's ok in this way.

db = SQLAlchemy(app)
session = db.session()
cursor = session.execute(sql).cursor
like image 30
Sucas Venior Avatar answered Sep 24 '22 17:09

Sucas Venior