From e2f9ee1e828870a6f09bb9027e1d901041fa81cb Mon Sep 17 00:00:00 2001 From: pzkn3atj5 Date: Fri, 26 May 2023 16:08:58 +0800 Subject: [PATCH] ADD file via upload --- db.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 db.py diff --git a/db.py b/db.py new file mode 100644 index 0000000..9e88789 --- /dev/null +++ b/db.py @@ -0,0 +1,45 @@ +""" + +代码功能:数据库链接与交互 +""" + +import pymysql +import hashlib +import Config as C + +global conn +conn = None + + +# 01获取数据库连接 +def conn_mysql(): + conn = None + try: + conn = pymysql.connect(host=C.DB_HOST, port=3306, user=C.DB_USER, password=C.DB_PASSWORD, db=C.DB_NAME, + charset='utf8') + except Exception as e: + print(e) + return conn + + +# 02根据SQL语句操作数据库 +def sql_execute(sql): + global conn + if conn is None: + conn = conn_mysql() + cur = conn.cursor() + cur.execute(sql) + result = cur.fetchall() + cur.close() + return result + + +# 03获取MD5加密结果 +def md5(text): + text = bytes(text, encoding='utf-8') + return hashlib.md5(text).hexdigest() + + +if __name__ == "__main__": + str_md5 = md5("123456") + print('MD5加密后为 :' + str_md5)