1

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.
5
  • 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 Commented Feb 5, 2014 at 1:00
  • You could add this as an answer so it will be easier to read. Commented Feb 5, 2014 at 1:01
  • Low reputation, doesn't let me answer my own question :( Commented Feb 5, 2014 at 1:03
  • Maybe you could try to edit the question to include the solution but I'm not sure that it is the right way to do it. Commented Feb 5, 2014 at 1:30
  • It let me answer it after 8 hours. Updated with answer. Commented Feb 5, 2014 at 17:44

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.