class MySqlClient:
def __init__(self, db_name: str):
self._conn = pymysql.connect(host=MYSQL_CONFIG['host'], port=MYSQL_CONFIG['port'],
user=MYSQL_CONFIG['user'], passwd=MYSQL_CONFIG['passwd'],
db=db_name,
use_unicode=True, charset='utf8')
def fetch_all_query_result(self, sql_patt: str, sql_args=()):
with self._conn:
with self._conn.cursor() as cursor:
cursor.execute(sql_patt, sql_args)
res = cursor.fetchall()
return res
def bulk_insert(self, sql_patt: str, values:Iterable[Iterable]):
with self._conn:
with self._conn.cursor() as cursor:
cursor.executemany(sql_patt, values)
self._conn.commit()
标签:patt,self,MySQL,CRUD,cursor,._,Client,sql,conn
From: https://www.cnblogs.com/LexLuc/p/17546099.html