samedi 3 octobre 2015

Get column names Dynamically with SQLAlchemy

I am querying sqlite db with SQLAlchemy like this:

import db

...

results = session.query(table).all()
        for result in results:
            print result
            print "how to print column name?"

Here is a snippet from db class:

class Device(Base):
    __tablename__ = "devices"
    name = Column(String(250), primary_key=True)
    location = Column(String(250), nullable=False)

    def __init__(self, name, location):
        self.name = name
        self.location = location

Attempting to use ".column_descriptions" on result as per the documentation throws "'list' object has no attribute 'column_descriptions'" error.

What is the proper way of getting column names dynamically along with the values? I want this so I can build a single function to handle json conversion of all queries instead of repeating code all over the place.

Aucun commentaire:

Enregistrer un commentaire