这一个读取数据文件批量写入数据库的python程序
时间: 2023-03-16 09:43:41 浏览: 188
Python从数据库读取大量数据批量写入文件的方法
我可以提供一些示例代码,以帮助你完成这个python程序:import mysql.connectormydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)mycursor = mydb.cursor()sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"with open('customers.csv', 'r') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
name = row[0]
address = row[1]
val = (name, address)
mycursor.execute(sql, val)mydb.commit()print(mycursor.rowcount, "记录插入到customers表中。")
阅读全文