首页 > 代码库 > Python 学习笔记 - sqlAlchemy(初稿)
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:yli@sydnagios: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:yli@sydnagios: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(初稿)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。