skip to Main Content

I have the following SQL database details:

import sqlalchemy as sch
from config import Config

db_uri = os.environ["SQLALCHEMY_DATABASE_URI"] + os.environ["DB_NAME"]

in the env file I have these

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://*****:*****@instance-amazonaws.com:3306/'
DB_NAME = 'DB_NAME'

Now

db_engine = extrac_db.create_engine(Config.db_uri)
db_connection = db_engine.connect()

When I try this query:

db_connection.execute("select count(*) from table")
query_result = db_connection.fetchall()

It gives the following error:

AttributeError: 'Connection' object has no attribute 'fetchall'

What is the problem here!!!?

2

Answers


  1. Wild guess:

    query = db_connection.execute("select count(*) from table")
    query_result = query.fetchall()
    
    Login or Signup to reply.
  2. This might work with SQLAlchemy

    import import sqlalchemy as sch
    import pprint
    
    with db_engine.begin() as conn:
        qry = sch.text("select count(*) from table")
        resultset = conn.execute(qry)
        results_as_dict = resultset.mappings().all()
        pprint(results_as_dict)
        
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search