0

How could I print column name and their values using SQLAlchemy in python?

For ex: I have a USER table, which contains 2 fields ID & NAME.

ID  NAME
1   John
2   Stella

I need something similar to this mysql query: select id as User_ID from USER where id = '1';

Currently, I am using the following query to get just the values:

meta = MetaData(engine)
users = Table('USER', meta, autoload=True)
selectQuery = select([users.c.ID, users.c.NAME]).where(users.c.ID == '1')

Executing above query I am getting:

('1', 'John')

Instead I need this:

ID  NAME
1   John
krutik
  • 1
  • 2
  • 1
    What exactly are you trying to do? You have the column names already, why can't you form your data from that? Just in case, a `Column` object has an attribute `name`, which is the name as text. Your "I need this" looks like the representation of a Pandas DataFrame. – Ilja Everilä Jan 18 '18 at 09:26
  • You should read ["What is the XY problem?"](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), so that in the future you may ask about the problem you're really trying to solve. – Ilja Everilä Jan 18 '18 at 10:48

0 Answers0