53 lines
1.5 KiB
Diff
53 lines
1.5 KiB
Diff
From 9d1aaac6870109b79ce77963f4270ba192a60a6f Mon Sep 17 00:00:00 2001
|
|
From: lixin <lixinb@uniontech.com>
|
|
Date: Mon, 6 Nov 2023 17:37:23 +0800
|
|
Subject: [PATCH] =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=95=B0=E6=8D=AE=E5=BA=93?=
|
|
MIME-Version: 1.0
|
|
Content-Type: text/plain; charset=UTF-8
|
|
Content-Transfer-Encoding: 8bit
|
|
|
|
---
|
|
connect_sql.py | 30 ++++++++++++++++++++++++++++++
|
|
1 file changed, 30 insertions(+)
|
|
create mode 100644 connect_sql.py
|
|
|
|
diff --git a/connect_sql.py b/connect_sql.py
|
|
new file mode 100644
|
|
index 0000000..e24a467
|
|
--- /dev/null
|
|
+++ b/connect_sql.py
|
|
@@ -0,0 +1,30 @@
|
|
+import pymysql
|
|
+
|
|
+class DBHelper:
|
|
+ # 构造函数
|
|
+ def __init__(self, host=db_host, user=db_user, pwd=db_password, db=db_name):
|
|
+ self.host = host
|
|
+ self.user = user
|
|
+ self.pwd = pwd
|
|
+ self.db = db
|
|
+ self.connect = None
|
|
+ self.cursor = None
|
|
+
|
|
+ # 连接数据库
|
|
+ def connectDatabase(self):
|
|
+ try:
|
|
+ self.connect = pymysql.connect(host=self.host, user=self.user, password=self.pwd, db=self.db,
|
|
+ charset='utf8')
|
|
+ except:
|
|
+ print('connectDatabase failed')
|
|
+ return False
|
|
+ self.cursor = self.connect.cursor()
|
|
+ return True
|
|
+
|
|
+ # 关闭数据库
|
|
+ def close(self):
|
|
+ # 如果数据打开,则关闭;否则没有操作
|
|
+ if self.connect and self.cursor:
|
|
+ self.cursor.close()
|
|
+ self.connect.close()
|
|
+ return True
|
|
--
|
|
2.20.1
|
|
|