pymysql插入数据库
时间: 2023-08-23 11:09:56 浏览: 117
您可以使用pymysql库将数据插入到数据库中。首先,您需要安装pymysql库,可以使用以下命令:
```
pip install pymysql
```
然后,您可以使用以下代码将数据插入到数据库中:
```python
import pymysql
# 连接数据库
connection = pymysql.connect(host='localhost',
user='your_username',
password='your_password',
db='your_database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# 创建一条插入数据的SQL语句
sql = "INSERT INTO your_table (column1, column2, column3) VALUES (%s, %s, %s)"
# 执行SQL语句
cursor.execute(sql, ('value1', 'value2', 'value3'))
# 提交事务
connection.commit()
finally:
# 关闭数据库连接
connection.close()
```
请替换`your_username`、`your_password`、`your_database`、`your_table`、`column1`、`column2`、`column3`以及对应的值为您的实际数据库信息和要插入的数据。这样就可以将数据成功插入到数据库中了。
阅读全文
相关推荐


















