在执行插入语句,每1000条提交一次
时间: 2024-03-10 12:43:54 浏览: 22
在执行插入语句时,为了提高效率和减少内存占用,可以每1000条数据提交一次。以下是一个 Python 的示例代码:
```python
import mysql.connector
# 连接到 MySQL 数据库
cnx = mysql.connector.connect(user='username', password='password',
host='localhost',
database='database_name')
cursor = cnx.cursor()
# 插入数据
for i in range(10000):
# 执行插入语句
insert_stmt = "INSERT INTO tablename (column1, column2, column3) VALUES (%s, %s, %s)"
data = ('value1', 'value2', 'value3')
cursor.execute(insert_stmt, data)
# 每1000条数据提交一次
if i % 1000 == 0:
cnx.commit()
# 最后提交一次,确保所有数据都已插入
cnx.commit()
# 关闭数据库连接
cursor.close()
cnx.close()
```
在上面的代码中,我们将每1000条数据提交一次,以确保在插入大量数据时不会出现内存占用问题,并且可以提高效率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)