import mysql.connector from mysql.connector import Error def insert_user(): try: # 连接到数据库 connection = mysql.connector.connect( host='127.0.0.1', user='root', password='mysql>hyx123', database='airquility' ) if connection.is_connected(): db_info = connection.get_server_info() print(f"Connected to MySQL Server version {db_info}") cursor = connection.cursor() # 从用户接收输入 user = input("Enter user: ") password = input("Enter password: ") # 准备插入数据的SQL语句 sql_insert_query = """INSERT INTO users (user,password) VALUES (%s, %s)""" # 用户输入的数据作为元组 insert_data = (user, password) # 执行插入操作 cursor.execute(sql_insert_query, insert_data) connection.commit() print("Record inserted successfully into users table.") except Error as e: print(f"Error while connecting to MySQL: {e}") finally: if (connection.is_connected()): cursor.close() connection.close() print("MySQL connection is closed") # 调用函数插入数据 insert_user()