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()

你可能感兴趣的:(SSCursor 处理大量数据)