Running a range query LIMIT a,b in TG/SQLAlchemy

22 views
Skip to first unread message

SamDonaldson

unread,
Jun 28, 2007, 8:41:34 AM6/28/07
to TurboGears
Hi,

I haven't been able to find an answer to this anywhere but how does
one run a mysql range type query where you have a LIMIT a, b?

Thanks

Sam

Christopher Arndt

unread,
Jun 28, 2007, 8:51:54 AM6/28/07
to turbo...@googlegroups.com
SamDonaldson schrieb:

> I haven't been able to find an answer to this anywhere but how does
> one run a mysql range type query where you have a LIMIT a, b?

SQLObject or SQLAlchemy?

For SO use slice notation:

results = mode.Foo.select(where_clause)[a:b]

For SA see:

http://www.sqlalchemy.org/docs/sqlconstruction.html#sql_whereclause_options

Chris

W-Mark Kubacki

unread,
Jun 28, 2007, 12:30:56 PM6/28/07
to turbo...@googlegroups.com
SamDonaldson wrote:
>
> I haven't been able to find an answer to this anywhere but how does
> one run a mysql range type query where you have a LIMIT a, b?

You can have the query printed by either adding "&debug=true" to your connection
string or by: (which is very handy at the shell)
Contact._connection.debug = True

Let's say you have an address book with Contact as model:
prq = Contact.select(Contact.q.last_name.startswith('Don'))
This won't actually execute the query but return something SQLObject will work with.

Now to your question, let's fetch some Contacts (with your a and b):
myList = prq[a:b]
Voila!

And, don't do this as it will yield in a query every iteration:
for person in prq: # bad!
...
Better, fetch all the results at once by:
for person in prq[:]: # good!
...
or:
for person in list(prq):
...

-- W-Mark Kubacki

Reply all
Reply to author
Forward
0 new messages