""" 需求:两个结构相同的表A, B,需要从 A表 将数据迁移到 B表 """ from peewee import * from playhouse.shortcuts import model_to_dict from conf import BaseModel # 配置好的数据库信息 class AModel(BaseModel): name = CharField(default="") age = IntegerField(default=0) class Meta: db_table = 'a_table' class BModel(BaseModel): name = CharField(default="") age = IntegerField(default=0) class Meta: db_table = 'b_table' if __name__ == '__main__': for page in range(1, 50): # 页数,要比实际页数多一页,以免数据漏掉 rows = AModel.select().paginate(page=page, paginate_by=1000) # 随机取出一定数量的数据 lst = [] for row in rows: dct = model_to_dict(row) dct.pop("id") # 删除字典中的id字段,这个默认会自增 # 通过 md5 字段判断,避免重复插入 ret = BModel.filter(BModel.md5 == dct["md5"]).first() if ret is None: lst.append(dct) if len(lst) > 0: BModel.insert_many(lst).execute() print "数据拷贝完成", "取出数据:%d" % len(rows), "成功插入%d" % len(lst) print "数据迁移完成"