Python 学习笔记 - sqlAlchemy(初稿)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 学习笔记 - sqlAlchemy(初稿)相关的知识,希望对你有一定的参考价值。
上一篇简单的介绍了MySQL的原生命令,以及如何使用pymysql模块在Python里面使用。这一节简单的介绍一下sqlAlchemy和他的ORM框架。ORM框架允许开发人员通过类的方法来操作数据库,而无需在使用原生的SQL语句。
例
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy import create_engine engine = create_engine("mysql+pymysql://yli:[email protected]:3306/mydb", max_overflow=5) Base = declarative_base() # 创建单表 class Users(Base): __tablename__ = ‘users‘ id = Column(Integer, primary_key=True) name = Column(String(32)) extra = Column(String(16)) __table_args__ = ( UniqueConstraint(‘id‘, ‘name‘, name=‘uix_id_name‘), Index(‘ix_id_name‘, ‘name‘, ‘extra‘), ) # 一对多 class Favor(Base): __tablename__ = ‘favor‘ nid = Column(Integer, primary_key=True) caption = Column(String(50), default=‘red‘, unique=True) class Person(Base): __tablename__ = ‘person‘ nid = Column(Integer, primary_key=True) name = Column(String(32), index=True, nullable=True) favor_id = Column(Integer, ForeignKey("favor.nid")) # 多对多 class ServerToGroup(Base): __tablename__ = ‘servertogroup‘ nid = Column(Integer, primary_key=True, autoincrement=True) server_id = Column(Integer, ForeignKey(‘server.id‘)) group_id = Column(Integer, ForeignKey(‘group.id‘)) class Group(Base): __tablename__ = ‘group‘ id = Column(Integer, primary_key=True) name = Column(String(64), unique=True, nullable=False) class Server(Base): __tablename__ = ‘server‘ id = Column(Integer, primary_key=True, autoincrement=True) hostname = Column(String(64), unique=True, nullable=False) port = Column(Integer, default=22) #创建表 Base.metadata.create_all(engine) #删除表 # Base.metadata.drop_all(engine)
上述命令创建的表
修改一下,我们可以添加数据和查询数据
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy import create_engine engine = create_engine("mysql+pymysql://yli:[email protected]:3306/mydb", max_overflow=5) Base = declarative_base() # 创建单表 class Users(Base): __tablename__ = ‘users‘ id = Column(Integer, primary_key=True) name = Column(String(32)) extra = Column(String(16)) __table_args__ = ( UniqueConstraint(‘id‘, ‘name‘, name=‘uix_id_name‘), Index(‘ix_id_name‘, ‘name‘, ‘extra‘), ) # 一对多 class Favor(Base): __tablename__ = ‘favor‘ nid = Column(Integer, primary_key=True) caption = Column(String(50), default=‘red‘, unique=True) class Person(Base): __tablename__ = ‘person‘ nid = Column(Integer, primary_key=True) name = Column(String(32), index=True, nullable=True) favor_id = Column(Integer, ForeignKey("favor.nid")) # 多对多 class ServerToGroup(Base): __tablename__ = ‘servertogroup‘ nid = Column(Integer, primary_key=True, autoincrement=True) server_id = Column(Integer, ForeignKey(‘server.id‘)) group_id = Column(Integer, ForeignKey(‘group.id‘)) class Group(Base): __tablename__ = ‘group‘ id = Column(Integer, primary_key=True) name = Column(String(64), unique=True, nullable=False) class Server(Base): __tablename__ = ‘server‘ id = Column(Integer, primary_key=True, autoincrement=True) hostname = Column(String(64), unique=True, nullable=False) port = Column(Integer, default=22) Base.metadata.create_all(engine) # Base.metadata.drop_all(engine) Session = sessionmaker(bind=engine) session = Session() obj = Users(name=‘alex111‘,extra=‘sss‘) session.add(obj) session.commit() q = session.query(Users) print(q) ret = session.query(Users).all() print(ret) print(ret[0].name) print(ret[0].id)
结果如下所示:
"C:\Program Files\Python3\python.exe" "C:/Users/yli/Documents/Tencent Files/38144205/FileRecv/FileRecv/day12/s17.py" SELECT users.id AS users_id, users.name AS users_name, users.extra AS users_extra FROM users [<__main__.Users object at 0x00000259D73AEF28>] alex111 1
本文出自 “麻婆豆腐” 博客,请务必保留此出处http://beanxyz.blog.51cto.com/5570417/1871672
以上是关于Python 学习笔记 - sqlAlchemy(初稿)的主要内容,如果未能解决你的问题,请参考以下文章
python 学习笔记十一 SQLALchemy ORM(进阶篇)