python实现和mysql一起实现数据库的增删改查

要在 Python 中使用 MySQL 数据库进行增删改查(CRUD)操作,你可以使用 `pymysql` 库。以下是一些示例代码,展示如何实现这些操作。

首先,确保你已经安装了 `pymysql`。如果没有安装,可以通过以下命令安装:

```bash

pip install pymysql

```

1. 连接数据库

```python

import pymysql

连接数据库

conn = pymysql.connect(host='localhost', user='your_username', password='your_password', db='your_db', charset='utf8mb4')

```

2. 创建(Create)

```python

创建游标对象

cursor = conn.cursor()

插入数据

sql = "INSERT INTO patient (name, age, time) VALUES (%s, %s, %s)"

try:

cursor.execute(sql, ('John Doe', 30, '2024-07-27 10:00:00'))

conn.commit() # 提交事务

except pymysql.Error as e:

print(e)

conn.rollback() # 回滚事务

关闭游标和连接

cursor.close()

conn.close()

```

3. 读取(Read)

```python

创建游标对象

cursor = conn.cursor(pymysql.cursors.DictCursor)

查询所有数据

sql = "SELECT * FROM patient"

try:

cursor.execute(sql)

results = cursor.fetchall()

for row in results:

print(row)

except pymysql.Error as e:

print(e)

关闭游标和连接

cursor.close()

conn.close()

```

4. 更新(Update)

```python

创建游标对象

cursor = conn.cursor()

更新数据

sql = "UPDATE patient SET age = %s WHERE name = %s"

try:

cursor.execute(sql, (31, 'John Doe'))

conn.commit() # 提交事务

except pymysql.Error as e:

print(e)

conn.rollback() # 回滚事务

关闭游标和连接

cursor.close()

conn.close()

```

5. 删除(Delete)

```python

创建游标对象

cursor = conn.cursor()

删除数据

sql = "DELETE FROM patient WHERE name = %s"

try:

cursor.execute(sql, ('John Doe',))

conn.commit() # 提交事务

except pymysql.Error as e:

print(e)

conn.rollback() # 回滚事务

关闭游标和连接

cursor.close()

conn.close()

```

完整示例

```python

import pymysql

连接数据库

conn = pymysql.connect(host='localhost', user='your_username', password='your_password', db='your_db', charset='utf8mb4')

创建游标对象

cursor = conn.cursor()

插入数据

sql_insert = "INSERT INTO patient (name, age, time) VALUES (%s, %s, %s)"

try:

cursor.execute(sql_insert, ('John Doe', 30, '2024-07-27 10:00:00'))

conn.commit()

except pymysql.Error as e:

print(e)

conn.rollback()

查询数据

sql_select = "SELECT * FROM patient"

try:

cursor.execute(sql_select)

results = cursor.fetchall()

for row in results:

print(row)

except pymysql.Error as e:

print(e)

更新数据

sql_update = "UPDATE patient SET age = %s WHERE name = %s"

try:

cursor.execute(sql_update, (31, 'John Doe'))

conn.commit()

except pymysql.Error as e:

print(e)

conn.rollback()

删除数据

sql_delete = "DELETE FROM patient WHERE name = %s"

try:

cursor.execute(sql_delete, ('John Doe',))

conn.commit()

except pymysql.Error as e:

print(e)

conn.rollback()

关闭游标和连接

cursor.close()

conn.close()

```

确保替换 `your_username`, `your_password`, `your_db` 为你的 MySQL 数据库的实际用户名、密码和数据库名。同时,确保你的数据库服务器正在运行,并且你有足够的权限执行这些操作。

相关推荐
RestCloud13 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud14 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence16 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger1 天前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud