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 数据库的实际用户名、密码和数据库名。同时,确保你的数据库服务器正在运行,并且你有足够的权限执行这些操作。

相关推荐
l1258654 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
梦Arrebol4 小时前
Mysql内容及相关实验
数据库·mysql
OceanWaves19934 小时前
mysql 8.0.32 磁盘爆满,清理从库日志
数据库·mysql
凤山老林4 小时前
数据库读写分离与动态路由实战:Spring Boot + ShardingSphere-JDBC 生产配置
数据库·spring boot·后端·分库分表·sharding-jdbc
l1258655 小时前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
聚美智数6 小时前
图片水印-图片剪裁-图片缩放API接口介绍
java·服务器·数据库
JavaPub-rodert6 小时前
Go 后台如何同时兼容 MySQL、PostgreSQL、SQLite 和 SQL Server?从 ShiyuAdmin 看 GORM 多数据库适配
数据库·mysql·postgresql·golang·javapub·王仕宇
瀚高PG实验室7 小时前
SQL优化案例:存储过程、复杂SQL拆成简单SQL提升整体性能
数据库·sql·postgresql·瀚高数据库
Code额7 小时前
基于 SQLAlchemy 2.x,面向 MySQL 数据库的从入门到精通实战教程(2)
数据库·sql·mysql·adb·ai
Wang's Blog7 小时前
PostgreSQL笔记36:执行计划基础解读与优化器成本模型
数据库·笔记·postgresql