import os import csv import mysql.connector def import_data_from_csv_to_mysql(): # 设置MySQL连接参数 mysql_host = "localhost" mysql_user = "root" mysql_password = "123456" mysql_database = "data" # 连接MySQL数据库 conn = mysql.connector.connect(host=mysql_host, user=mysql_user, password=mysql_password, database=mysql_database) # 获取MySQL游标 cursor = conn.cursor() # 准备SQL语句 sql = "INSERT INTO" " congtent (title,comment_num,price, is_self_support,args0,args1,args2,totalCount,picFlagCount,five_fourStarCount,two_threeStarCount,oneStarCount,againCount,comments,datas,users,styles,colors,label)" " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" # 打开CSV文件并读取数据 with open('congtent.csv', encoding='utf-8') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') next(csv_reader) for row in csv_reader: # 插入数据到MySQL数据库 cursor.execute(sql, tuple(row)) # 提交数据到MySQL数据库 conn.commit() # 关闭连接和游标 cursor.close() conn.close() if __name__ == '__main__': import_data_from_csv_to_mysql() print("CSV数据导入MySQL数据库成功!")