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