From 42c4d2dc4f7f606d24f27f1a701677c48cc59fad Mon Sep 17 00:00:00 2001 From: pzyexn7bj <2558997203@qq.com> Date: Fri, 31 May 2024 10:34:35 +0800 Subject: [PATCH] =?UTF-8?q?MySQL=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MysqlUtil.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 MysqlUtil.py diff --git a/MysqlUtil.py b/MysqlUtil.py new file mode 100644 index 0000000..4177228 --- /dev/null +++ b/MysqlUtil.py @@ -0,0 +1,32 @@ +import pymysql + + +def connect_to_mysql(): + return pymysql.connect( + host='localhost', + port=3306, + user='root', + password='root', + database='pythontest', + autocommit=True, # 自动提交 + charset='utf8mb4' + ) + + +def query_sql(sql): + conn = connect_to_mysql() + try: + cursor = conn.cursor() + cursor.execute(sql) + return cursor.fetchall() + finally: + conn.close() + + +def not_query_sql(sql): + conn = connect_to_mysql() + try: + cursor = conn.cursor() + cursor.execute(sql) + finally: + conn.close() \ No newline at end of file