首页 > 技术文章 > python 数据库游标,fetchmany(int),边取数据边处理,防止数量量太大撑爆内存

qianxunman 2020-11-16 10:25 原文

        self.cr.execute(sql, )
        # res = self.cr.fetchmany(1000)
        while True:
            res = self.cr.fetchmany(1000)
            print(len(res),res)
            if not res:
                break
        return res
  1. fetchmany 和fetchall() 类似,只是可以读取指定行数的数据量,当指定数据量小于实际数据量时,可以重复读取
  2. 如果使用fetchall() 来读取数据,如果数据量太大,放在一个列表中,可能放不下,就可以使用fetchmany()分批读取的方式.

推荐阅读