Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select batch of rows sqlalchemy mysql

I have a MySQL database with a few thousand forum posts + text. I would like to grab them in batches, say 1000 at a time, and do stuff to them in python3.

My single post query looks like:

pquery = session.query(Post).\
    filter(Post.post_id.like(post_id))

How can I change this so that given a post_id, it returns that post and the 999 posts after it?

like image 618
nicolashahn Avatar asked Sep 08 '15 19:09

nicolashahn


1 Answers

Use limit and offset:

pquery = session.query(Post).filter(Post.post_id.like(post_id)).limit(1000).offset(the_offset_val)
like image 68
John Avatar answered Nov 07 '22 06:11

John