SSCursor 处理大量数据

使用游标的好处是不会将查询结果全部都放入内存中,避免了占用大量的内存,会从存储块中读取记录,并且一条一条的返回来

class DbConnection(object):

def __init__(self,host,port,username,password,db):

self.host = host
self.port = port
self.username = username
self.password = password
self.db = db
self.connection = None

def __enter__(self):
# 建立连接
self.connection = pymysql.connect(host = self.host, port= self.port, user=self.username, passwd=self.password,
db = self.db)
#使用流式游标
self.cursor = self.connection.cursor(pymysql.cursors.SSDictCursor)
return self.cursor

def __exit__(self, exc_type, exc_val, exc_tb):
self.connection.commit()
self.cursor.close()
self.connection.close()

posted on 2019-08-19 21:18  yingchen  阅读(839)  评论(0编辑  收藏  举报

导航