9

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?

nicolashahn
  • 539
  • 7
  • 19

1 Answers1

10

Use limit and offset:

pquery = session.query(Post).filter(Post.post_id.like(post_id)).limit(1000).offset(the_offset_val)
John
  • 2,410
  • 1
  • 19
  • 33
  • 2
    Also, there is a `slice(from, to)` option http://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.slice – alisianoi Jan 27 '18 at 20:11