bluetooth
bluetooth

Reputation: 559

Reading a database table without Pandas

I am trying to read a table from a HANA database in Python using SQLAlchemy library. Typically, I would use the Pandas package and use the pd.read_sql() method for this operation. However, for some reason, the environment I am using does not support the Pandas package. Therefore, I need to read the table without the Pandas library. So far, the following is what I have been able to do:

query = ('''SELECT * FROM "<schema_name>"."<table_name>"''' 
        ''' WHERE <conditional_clauses>'''
       )
            
with engine.connect() as con:
    table = con.execute(query)
row = table.fetchone()

However, while this technique allows me to read table row by row, I am do not get the column names of the table.

How can I fix this?

Thanks

Upvotes: 2

Views: 1455

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123549

I am do not get the column names of the table

You won't get the column names of the table but you can get the column names (or aliases) of the result set:

with engine.begin() as conn:
    row = conn.execute(sa.text("SELECT 1 AS foo, 2 AS bar")).fetchone()
    print(row.items())  # [('foo', 1), ('bar', 2)]
    #
    # or, for just the column names
    #
    print(row.keys())  # ['foo', 'bar']

Upvotes: 1

Related Questions