Mysql sql插入10w条数据
时间: 2023-09-18 07:14:40
浏览: 235

Mysql使用insert插入多条记录 批量新增数据
可以使用以下命令来向 [mysql](https://wenku.csdn.net/doc/6401abb7cce7214c316e93d1?spm=1055.2569.3001.10083) 数据库中插入 10w 条数据:
```sql
INSERT INTO [table](https://wenku.csdn.net/doc/5th6zsbvjq?spm=1055.2569.3001.10083)_name (column1, column2, column3, ...)
VALUES
(value1, value2, value3, ...),
(value1, value2, value3, ...),
(value1, value2, value3, ...);
其中,`table_name` 是你要插入数据的表名,`column1, column2, column3, ...` 是表中的列名,`value1, value2, value3, ...` 是要插入的数据值。
你可以使用循环语句生成大量的数据,然后将它们一次性插入到表中。例如,以下是使用 [python](https://wenku.csdn.net/doc/6412b46ebe7fbd1778d3f92a?spm=1055.2569.3001.10083) 在 MySQL 中插入 10w 条数据的示例代码:
```python
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
[database](https://wenku.csdn.net/doc/6412b5fcbe7fbd1778d451aa?spm=1055.2569.3001.10083)="yourdatabase"
# 创建游标对象
mycursor = mydb.cursor()
# 生成数据并插入到表中
for i in range(100000):
sql = "INSERT INTO [custom](https://wenku.csdn.net/doc/7b8fcq5ew4?spm=1055.2569.3001.10083)ers (name, address) VALUES (%s, %s)"
val = ("Customer " + str(i), "Address " + str(i))
mycursor.execute(sql, val)
# 提交更改
mydb.commit()
# 打印插入的数据行数
print(mycursor.rowcount, "记录插入成功。")
以上代码会向名为 `customers` 的表中插入 10w 条数据,每条数据包括 `name` 和 `address` 两个列。你可以根据需要修改表名和列名。
阅读全文
相关推荐















