python 数据库游标,fetchmany(int),边取数据边处理,防止数量量太大撑爆内存

时间:2020-11-20 12:06:46   收藏:0   阅读:12
        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()分批读取的方式.
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!