If the column names are part of the "schools_master" table, then the
easiest option is something like this:
q = session.query(AAA).limit(100)
for row in q:
for name in col_names:
print '%s = %' % (name, getattr(row, name))
Alternatively:
cols = [getattr(AAA, name) for name in col_names]
q = session.query(*cols).limit(100)
for row in q:
print row
Simon
On Wed, Apr 5, 2017 at 1:20 PM, Mayank Soni <[email protected]> wrote:
> Thanks Simon for response.
> Actually column names I am getting from user interface based on user
> selection that is why i want pass columns in query method dynamically. can
> you suggest me how can achieve this.
>
>
> On Wednesday, April 5, 2017 at 5:08:50 PM UTC+5:30, Simon King wrote:
>>
>> On Wed, Apr 5, 2017 at 11:10 AM, Mayank Soni <[email protected]> wrote:
>> > I am trying to pass list of columns of table into query method using
>> > add_columns method. Below i am mentioning code snipped.
>> >
>> > def LLL():
>> > Base = automap_base()
>> > class AAA(Base):
>> > __tablename__ = 'schools_master'
>> > Base.prepare(engine, reflect=True)
>> > session = sessionmaker(bind=engine)
>> > session = session()
>> > col_name =
>> >
>> > ['dise_code_01','district_name_02','block_name_03','cluster_name_04','village_name_05']
>> >
>> > q = session.query(AAA).add_columns(col_list)
>> >
>> > q = q.limit(100)
>> >
>> > for row in q:
>> > print(row)
>> >
>> > When i execute this code it is generating error massage :
>> > massage.sqlalchemy.exc.InvalidRequestError: SQL expression, column, or
>> > mapped entity expected - got '['dise_code_01', 'district_name_02',
>> > 'block_name_03', 'cluster_name_04', 'village_name_05']
>> >
>> > Please help me out from this error.
>> >
>>
>> add_columns expects each column to be supplied as a separate argument,
>> so you at least need:
>>
>> q = session.query(AAA).add_columns(*col_list)
>>
>> ...to unpack the list.
>>
>> Hope that helps,
>>
>> Simon
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.