脚本导入MySQL数据是指通过编写脚本程序,将数据批量导入到MySQL数据库中。这种操作通常用于数据迁移、数据备份恢复、数据初始化等场景。脚本可以是任何支持与MySQL交互的编程语言编写的,如Python、PHP、Java等。
以下是一个使用Python和mysql-connector-python库批量导入数据的示例:
import mysql.connector
# 连接到MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="yourdatabase"
)
mycursor = mydb.cursor()
# 准备要插入的数据
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('John', 'Highway 21'),
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
# 执行批量插入
mycursor.executemany(sql, val)
# 提交事务
mydb.commit()
print(mycursor.rowcount, "记录已插入。")通过以上方法,可以有效地解决脚本导入MySQL数据过程中遇到的问题。
没有搜到相关的文章