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()