MySQL CRUD Client


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

posted @ 2023-07-11 22:12  LexLuc  阅读(3)  评论(0编辑  收藏  举报