17 lines
518 B
Python
17 lines
518 B
Python
import mysql.connector
|
|
|
|
class ConnMysql:
|
|
def __init__(self, ret="conn", **kwargs):
|
|
self.ret = ret
|
|
self.conn = mysql.connector.connect(**kwargs, autocommit=False)
|
|
if ret == "cursor":
|
|
self.cursor = self.conn.cursor()
|
|
|
|
def __enter__(self):
|
|
return self.cursor if self.ret == "cursor" else self.conn
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
if self.ret == "cursor":
|
|
self.cursor.close()
|
|
self.conn.__exit__(exc_type, exc_val, exc_tb)
|