Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLALCHEMY - Iterating through data

When I am using SQLALchemy how would one iterate through column names?

Eg.

Column Name 1, Column Name 2, Column Name 3, etc...

The second question is I have the following query:

root = dbsession.query(MyTable).filter(MyTable.name==u'john').all()

However, when I do:

for row in root:
    print row

I don't get any results. Instead I have to do:

print row.name, row.age, etc...

Can't I just do print row to return data from all the columns?

like image 831
sidewinder Avatar asked Jun 22 '11 20:06

sidewinder


1 Answers

dbsession.query(MyTable).filter(MyTable.name==u'john') will go through ORM and return you objects. If you just want to return all columns, you can bypass ORM with this:

query = dbsession.query(MyTable).filter(MyTable.name==u'john')
rows = query.statement.execute().fetchall()
for row in rows:
    print row
like image 200
sayap Avatar answered Sep 18 '22 16:09

sayap