MySQL是一种关系型数据库管理系统,用于存储、管理和检索数据。数据导入是将外部数据加载到MySQL数据库中的过程。这通常涉及将数据从一个文件(如CSV、SQL脚本等)导入到数据库表中。
LOAD DATA INFILE
命令。原因:
解决方案:
import mysql.connector
# 连接数据库
db = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
cursor = db.cursor()
# 打开CSV文件
with open('data.csv', 'r') as file:
csv_reader = csv.reader(file)
next(csv_reader) # 跳过标题行
for row in csv_reader:
cursor.execute("INSERT INTO yourtable (column1, column2, column3) VALUES (%s, %s, %s)", row)
# 提交事务
db.commit()
# 关闭连接
cursor.close()
db.close()
通过以上方法,可以有效提高MySQL数据导入的速度,并解决常见的导入问题。
领取专属 10元无门槛券
手把手带您无忧上云