limit number of rows peewee retrieve

半世苍凉 提交于 2019-12-11 00:14:45

问题


I have the following python source file to retrieve rows from MySQL database. The problem is there are too many rows to retrieve in subscriber table. How can I do limit to small chunk of rows to retrieve and process and so on?

class subinfo(peewee.Model):
    sub_id = peewee.IntegerField()
    active = peewee.BooleanField()
    sub_type = peewee.IntegerField()
    sub_cat = peewee.TextField()

    class Meta:
        database = locdb

class subscriber(peewee.Model):
    sub_id = peewee.IntegerField(unique=True, primary_key=True)
    sub_start = peewee.DateTimeField()
    sub_end = peewee.DateTimeField()
    ref_id = peewee.BigIntegerField()
    class Meta:
        database = locdb

for row in subscriber.select():
    if(row.sub_start >= peewee.datetime.datetime.now()):
        sub1, created = subinfo.get_or_create(sub_id=row.sub_id, active=True)
        if(created == False & sub1.active == False):
            q = subinfo.update(active=True).where(sub_id=row.sub_id)
            q.execute()
    else:
        subinfo.get_or_create(sub_id=row.sub_id, active=False)

回答1:


for row in subscriber.select()

Do this for first 100 rows:

 for row in subscriber.select().limit(100)

Next 100 rows:

 for row in subscriber.select().limit(100).offset(100)

Etc.



来源:https://stackoverflow.com/questions/45503451/limit-number-of-rows-peewee-retrieve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!