gprx100
gprx100

Reputation: 360

select a single column from Mysql DB using sqlalchemy

How do I get values from a single column using sqlalchemy?

In MySQL

select id from request r where r.product_id = 1;

In Python

request = meta.tables['request']
request.select(request.c.product_id==1).execute().rowcount   
27L

>>> request.select([request.c.id]).where(request.c.product_id==1).execute()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.freebsd-6.3-RELEASE-i386/egg/sqlalchemy/sql/expression.py", line 2616,     in select
File "build/bdist.freebsd-6.3-RELEASE-i386/egg/sqlalchemy/sql/expression.py", line 305, in select
File "build/bdist.freebsd-6.3-RELEASE-i386/egg/sqlalchemy/sql/expression.py", line 5196, in __init__
File "build/bdist.freebsd-6.3-RELEASE-i386/egg/sqlalchemy/sql/expression.py", line 1517, in _literal_as_text
sqlalchemy.exc.ArgumentError: SQL expression object or string expected.

Upvotes: 1

Views: 870

Answers (1)

gprx100
gprx100

Reputation: 360

I found the answer, I have to use the general select vs the table select.

Leaving this incase more folks find it useful.

conn = engine.connect()
stmt = select([request.c.id]).where(request.c.product_id==1)
conn.execute(stmt).rowcount
27L

Upvotes: 2

Related Questions