Commit 9acf0300 by 柴鹏飞

新增数据库连接类

parent 4bd5fb43
[ADB]
host = am-bp1w063w3g4908x1890650o.ads.aliyuncs.com
port = 3306
user =
password =
# run: conda env create --file environment.yaml
name: yar
channels:
- defaults
dependencies:
- python==3.8
- ipykernel
- pip
- pip:
- -r requirements.txt
\ No newline at end of file
# -*- coding: utf-8 -*-
import pymysql
class MySQLClient():
def __init__(self, host, port, user, password, logger=None) -> None:
self.logger = logger
self.connection = pymysql.connect(
host=host,
port=port,
user=user,
password=password,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
self.cursor = self.connection.cursor()
if self.logger:
self.logger.info('数据库连接成功')
def query(self, sql):
row_count = self.cursor.execute(sql)
data = self.cursor.fetchall()
return row_count, data
def __del__(self):
try:
self.cursor.close()
self.connection.close()
print('dataset disconnected')
except Exception as e:
print(e)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment