Python 数据库操作实战:使用 SQLAlchemy ORM 完成增删改查
数据库是应用开发的重要组成部分,而 ORM(对象关系映射)技术能够帮助我们用面向对象的思维操作数据库。
本篇文章将以 Python 中最流行的 ORM 框架 SQLAlchemy 为例,详细讲解如何搭建模型并实现增删改查操作。
一、安装依赖
bash
复制编辑
pip install sqlalchemy
若需要连接 MySQL 等数据库,还需安装对应驱动,例如:
bash
复制编辑
pip install pymysql
二、创建数据库连接与模型定义
python
复制编辑
from sqlalchemy import create_engine, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker # 创建数据库连接(以 SQLite 为例) engine = create_engine('sqlite:///test.db', echo=True) Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String(50)) age = Column(Integer) def __repr__(self): return f"<User(name='{self.name}', age={self.age})>" Base.metadata.create_all(engine) Session = sessionmaker(bind=engine) session = Session()
三、增删改查操作示例
1. 添加数据
python
复制编辑
user1 = User(name='花花', age=28) session.add(user1) session.commit() print("添加成功")
2. 查询数据
python
复制编辑
users = session.query(User).filter(User.age > 20).all() for user in users: print(user)
3. 更新数据
python
复制编辑
user = session.query(User).filter_by(name='花花').first() if user: user.age = 29 session.commit() print("更新成功")
4. 删除数据
python
复制编辑
user = session.query(User).filter_by(name='花花').first() if user: session.delete(user) session.commit() print("删除成功")
四、总结
| 操作 | 说明 |
|---|---|
session.add() |
新增对象到数据库 |
session.query() |
查询数据 |
session.commit() |
提交事务 |
session.delete() |
删除对象 |
SQLAlchemy 作为功能强大的 ORM,不仅支持多数据库,还支持复杂关系映射,推荐在项目中使用。
https://bigu.wang
https://www.bigu.wang
https://binm.wang
https://www.binm.wang
https://bint.wang
https://www.bint.wang
https://biop.wang
https://www.biop.wang
https://bits.wang
https://www.bits.wang
https://bjqb.wang
https://www.bjqb.wang
https://bjsm.wang
https://www.bjsm.wang
https://bleo.wang
https://www.bleo.wang
https://ono.wang
https://www.ono.wang
https://onz.wang
https://www.onz.wang
https://opo.wang
https://www.opo.wang
https://osm.wang
https://www.osm.wang
https://osn.wang
https://www.osn.wang
https://ovi.wang
https://www.ovi.wang
https://oxq.wang
https://www.oxq.wang
https://oti.wang
https://www.oti.wang
https://owu.wang
https://www.owu.wang
https://piq.wang
https://www.piq.wang
https://qmi.wang
https://www.qmi.wang
https://qki.wang
https://www.qki.wang
https://ref.wang
https://www.ref.wang
https://sak.wang
https://www.sak.wang
https://sar.wang
https://www.sar.wang
https://sfa.wang
https://www.sfa.wang
https://sfe.wang
https://www.sfe.wang
https://sgo.wang
https://www.sgo.wang
https://sku.wang
https://www.sku.wang
https://ycxjz.cn
https://www.ycxjz.cn
https://bnbmhomes.cn
https://www.bnbmhomes.cn
https://jinjianzuche.com
https://www.jinjianzuche.com
https://ahswt.cn
https://www.ahswt.cn
https://szwandaj.cn
https://www.szwandaj.cn
https://psbest.cn
https://www.psbest.cn
https://shanghai-arnold.cn
https://www.shanghai-arnold.cn
https://zgsscw.com
https://www.zgsscw.com
https://shxqth.cn
https://www.shxqth.cn
https://wdxj.cn
https://www.wdxj.cn
https://jad168.com
https://www.jad168.com
https://ultratrailms.cn
https://www.ultratrailms.cn
https://tztsjd.cn
https://www.tztsjd.cn
https://csqcbx.cn
https://www.csqcbx.cn
https://qazit.cn
https://www.qazit.cn
https://ahzjyl.cn
https://www.ahzjyl.cn
更多推荐


所有评论(0)