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

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

相关文章:

  • 2021-12-26
  • 2021-11-06
  • 2022-01-28
  • 2022-12-23
  • 2021-09-12
  • 2021-07-01
  • 2021-11-15
猜你喜欢
  • 2022-12-23
  • 2021-09-22
  • 2022-02-20
  • 2021-07-29
  • 2022-12-23
  • 2022-01-18
  • 2022-02-22
相关资源
相似解决方案