import mysql.connector
class MysqlConc:
def __init__(self, user, password, host, port, database):
self.user = user
self.password = password
self.host = host
self.port = port
self.database = database
self.cursor = None
self.cnx = None
def __enter__(self):
self.cnx = mysql.connector.connect(
user=self.user,
password=self.password,
host=self.host,
port=self.port,
database=self.database,
buffered=True)
self.cursor = self.cnx.cursor()
return self.cursor
def __exit__(self, p1, p2, p3):
if self.cnx:
self.cnx.close()
print "close mysql cnx"
def get_mysql(user, password, host, port, database):
return MysqlConc(user, password, host, port, database)
Python实现MySQL链接用with
最新推荐文章于 2023-03-25 16:38:45 发布