SQLAlchemy ORM框架

连表操作

  • 一对多

    • 创建表 指定约束 ForeignKey

    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() # 一对多
    class Group(Base):
    __tablename__ = "group"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    caption = Column # 组名 class User(Base):
    __tablename__ = "user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32))
    group_id = Column(Integer, ForeignKey("group.nid")) # 外键 Base.metadata.create_all(engine)
    • 插入数据
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() # 单表
    class Test(Base):
    __tablename__ = "test"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(32)) # 一对多
    class Group(Base):
    __tablename__ = "group"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    caption = Column(String(32)) # 组名 class User(Base):
    __tablename__ = "user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32))
    group_id = Column(Integer, ForeignKey("group.nid")) # 外键 # Base.metadata.create_all(engine) # 创建表
    # Base.metadata.drop_all(engine) # 删除表 Session = sessionmaker(bind=engine)
    session = Session() ### 添加数据
    # 向表中添加单条数据
    session.add(Group("dba"))
    session.add(Group("ddd")) # 向表中添加多条数据
    session.add_all(
    [
    User(username="a", group_id=1),
    User(username="b", group_id=2),
    User(username="c", group_id=1)
    ]
    ) session.commit()
    • 查询数据

    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() ### 创建表结构
    # 单表
    class Test(Base):
    __tablename__ = "test"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(32)) # 一对多
    class Group(Base):
    __tablename__ = "group"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    caption = Column(String(32)) # 组名 class User(Base):
    __tablename__ = "user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32))
    group_id = Column(Integer, ForeignKey("group.nid")) # 外键 # 与生成表结构无关,仅用于更方便的进行连表查询
    group = relationship("Group", backref="user") def __repr__(self):
    temp = "%s - %s - %s" % (self.nid, self.username, self.group_id)
    return temp # Base.metadata.create_all(engine) # 创建表
    # Base.metadata.drop_all(engine) # 删除表 Session = sessionmaker(bind=engine)
    session = Session() """
    ### 添加数据
    # 向表中添加单条数据
    session.add(Group("dba"))
    session.add(Group("ddd")) # 向表中添加多条数据
    session.add_all(
    [
    User(username="a", group_id=1),
    User(username="b", group_id=2),
    User(username="c", group_id=1)
    ]
    ) session.commit()
    """ # # 查询单表中所有数据
    # ret = session.query(User).all()
    # print(ret) # 输出User类中__repr__方法中返回的字符串
    # for obj in ret:
    # print(obj.nid, obj.username, obj.group_id)
    #
    # # 通过指定列查询
    # ret = session.query(User.nid, User.username).all()
    # print(ret) # 返回一个列表,列表中包含每一行的数据 [(2, 'a'), (3, 'b'), (4, 'c')] # # 根据指定条件查询
    # ret = session.query(User).filter(User.username == "a").all()
    # obj = ret[0]
    # print(obj.nid, obj.username, obj.group_id) # 连表查询 # 通过join进行连表查询
    # ret = session.query(User).join(Group) # 返回执行的sql语句
    # print(ret)
    # print("------")
    # ret = session.query(User).join(Group).all()
    # for obj in ret:
    # print(obj.nid, obj.username, obj.group_id)
    #
    # ret = session.query(User, Group).join(Group).all()
    # print(ret)
    #
    #
    # ret = session.query(User.username, Group.caption).join(Group).all()
    # print(ret) # 通过relationship在创建表的类中指定连表的对应关系,然后进行连表查询
    # 正向查询 (正向查找不能够过滤组的字段)
    ret = session.query(User).all()
    for obj in ret:
    # obj代指user表中的每一行数据
    # obj.group代指obj当前对应的group表中数据的对象
    print(obj.nid, obj.username, obj.group_id, obj.group, obj.group.nid, obj.group.caption) # 反向查询
    # 查找所有组名为dba的用户信息
    obj = session.query(Group).filter(Group.caption=="dba").first()
    print(obj.nid, obj.caption, obj.user) # obj.user 这个地方的user是在User类中使用relationship创建对应关系中 backref对应的值
  • 多对多

    • 原始方法和新式方法1
    ### 关系在第三张表中
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() class Host(Base):
    """
    主机表
    """
    __tablename__ = "host"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    hostname = Column(String(32))
    port = Column(String(32))
    ip = Column(String(32)) class HostUser(Base):
    """
    登录主机的用户表
    """
    __tablename__ = "host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32)) class Host_HostUser(Base):
    """
    关系表
    """
    __tablename__ = "host_to_host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    host_id = Column(Integer, ForeignKey("host.nid"))
    host_user_id = Column(Integer, ForeignKey("host_user.nid")) host = relationship("Host", backref="h")
    host_user = relationship("HostUser", backref="u") # Base.metadata.create_all(engine) # 创建表 Session = sessionmaker(bind=engine)
    session = Session() # # 向host主机表中添加多条数据
    # session.add_all(
    # [
    # Host(hostname="c1", port="22", ip="1.1.1.1"),
    # Host(hostname="c2", port="22", ip="1.1.1.2"),
    # Host(hostname="c3", port="22", ip="1.1.1.3"),
    # ]
    # ) # # 向host_user登录主机用户表中添加多条数据
    # session.add_all(
    # [
    # HostUser(username="root"),
    # HostUser(username="aa"),
    # HostUser(username="bb"),
    # HostUser(username="cc"),
    # ]
    # ) # # 向host_to_host_user关系表中添加多条数据
    # session.add_all(
    # [
    # Host_HostUser(host_id=1, host_user_id=1),
    # Host_HostUser(host_id=1, host_user_id=2),
    # Host_HostUser(host_id=1, host_user_id=3),
    # Host_HostUser(host_id=1, host_user_id=4),
    # Host_HostUser(host_id=2, host_user_id=2),
    # Host_HostUser(host_id=2, host_user_id=4),
    # Host_HostUser(host_id=3, host_user_id=1),
    # Host_HostUser(host_id=3, host_user_id=3),
    # ]
    # ) # session.commit() # 需求:找出能够登录主机名为c1的所有用户
    # ### 原始方式
    # # 从host表中将找到主机名为c1的nid
    # host_obj = session.query(Host).filter(Host.hostname == "c1").first()
    #
    # # 通过主机名为c1的nid在关系表中找到用户的id
    # host_2_host_user = session.query(Host_HostUser.host_user_id).filter(Host_HostUser.host_id==host_obj.nid).all()
    #
    # r = zip(*host_2_host_user)
    # users = session.query(HostUser.username).filter(HostUser.nid.in_(list(r)[0])).all()
    # print(users) ### 原始方式结合
    ret = session.query(HostUser.username).filter(HostUser.nid.in_(session.query(Host_HostUser.host_user_id).filter(Host_HostUser.host_id == session.query(Host.nid).filter(Host.hostname == 'c1')))).all()
    print(ret) ### 新式方法
    # 通过在Host_HostUser类中使用relationship建立与Host类和HostUser类建立关系,然后使用反向查询和正向询进行查找
    host_obj = session.query(Host).filter(Host.hostname=="c1").first()
    for item in host_obj.h: # host_obj.h 反向查询
    print(item.host_user.username) # item.host_user.username 正向查询
    • 新式方法2
    ### 关系在第1张表中 secondary=Host_HostUser.__table__ 要将关系表放在最上面 如果不使用lambda则应该将关系表放在最上面
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() class Host_HostUser(Base):
    """
    关系表
    """
    __tablename__ = "host_to_host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    host_id = Column(Integer, ForeignKey("host.nid"))
    host_user_id = Column(Integer, ForeignKey("host_user.nid")) class Host(Base):
    """
    主机表
    """
    __tablename__ = "host"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    hostname = Column(String(32))
    port = Column(String(32))
    ip = Column(String(32)) host_user = relationship("HostUser",
    secondary=Host_HostUser.__table__,
    backref="h") class HostUser(Base):
    """
    登录主机的用户表
    """
    __tablename__ = "host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32)) # Base.metadata.create_all(engine) # 创建表 Session = sessionmaker(bind=engine)
    session = Session() # # 向host主机表中添加多条数据
    # session.add_all(
    # [
    # Host(hostname="c1", port="22", ip="1.1.1.1"),
    # Host(hostname="c2", port="22", ip="1.1.1.2"),
    # Host(hostname="c3", port="22", ip="1.1.1.3"),
    # ]
    # ) # # 向host_user登录主机用户表中添加多条数据
    # session.add_all(
    # [
    # HostUser(username="root"),
    # HostUser(username="aa"),
    # HostUser(username="bb"),
    # HostUser(username="cc"),
    # ]
    # ) # # 向host_to_host_user关系表中添加多条数据
    # session.add_all(
    # [
    # Host_HostUser(host_id=1, host_user_id=1),
    # Host_HostUser(host_id=1, host_user_id=2),
    # Host_HostUser(host_id=1, host_user_id=3),
    # Host_HostUser(host_id=1, host_user_id=4),
    # Host_HostUser(host_id=2, host_user_id=2),
    # Host_HostUser(host_id=2, host_user_id=4),
    # Host_HostUser(host_id=3, host_user_id=1),
    # Host_HostUser(host_id=3, host_user_id=3),
    # ]
    # ) # session.commit() # 需求:找出能够登录主机名为c1的所有用户
    ### 新式方法二 host_obj = session.query(Host).filter(Host.hostname=="c1").first()
    print(host_obj.host_user)
    • 新式方法三
    ### secondary="host_to_host_user" 关系表无顺序要求 如果不使用lambda则应该将关系表放在最上面
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() class Host(Base):
    """
    主机表
    """
    __tablename__ = "host"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    hostname = Column(String(32))
    port = Column(String(32))
    ip = Column(String(32)) host_user = relationship("HostUser",
    secondary="host_to_host_user",
    backref="h") class HostUser(Base):
    """
    登录主机的用户表
    """
    __tablename__ = "host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32)) class Host_HostUser(Base):
    """
    关系表
    """
    __tablename__ = "host_to_host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    host_id = Column(Integer, ForeignKey("host.nid"))
    host_user_id = Column(Integer, ForeignKey("host_user.nid")) # Base.metadata.create_all(engine) # 创建表 Session = sessionmaker(bind=engine)
    session = Session() ### 新式方法三 host_obj = session.query(Host).filter(Host.hostname=="c1").first()
    print(host_obj.host_user)
    • 新式方法四
    ### secondary=lambda : Host_HostUser.__table__  如果不使用lambda则应该将关系表放在最上面
    
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() class Host(Base):
    """
    主机表
    """
    __tablename__ = "host"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    hostname = Column(String(32))
    port = Column(String(32))
    ip = Column(String(32)) host_user = relationship("HostUser",
    secondary=lambda : Host_HostUser.__table__,
    backref="h") class HostUser(Base):
    """
    登录主机的用户表
    """
    __tablename__ = "host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32)) class Host_HostUser(Base):
    """
    关系表
    """
    __tablename__ = "host_to_host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    host_id = Column(Integer, ForeignKey("host.nid"))
    host_user_id = Column(Integer, ForeignKey("host_user.nid")) # Base.metadata.create_all(engine) # 创建表 Session = sessionmaker(bind=engine)
    session = Session() ### 新式方法四 host_obj = session.query(Host).filter(Host.hostname=="c1").first()
    print(host_obj.host_user)
    • 新式方法五
    ### 关系表需要使用Table()进行创建
    ### relationship 中secondary=Host_HostUser, 如果不使用lambda则应该将关系表放在最上面 from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index, Table
    from sqlalchemy.orm import sessionmaker, relationship engine = create_engine("mysql+pymysql://tuocigaoshou:Eb^BEF38E9FBC36CA775@111.204.117.99:3306/test", max_overflow=5) Base = declarative_base() Host_HostUser = Table('host_to_host_user', Base.metadata,
    Column('nid', Integer, primary_key=True, autoincrement=True),
    Column('host_id', Integer, ForeignKey("host.nid")),
    Column('host_user_id', Integer, ForeignKey("host_user.nid")),
    ) class Host(Base):
    """
    主机表
    """
    __tablename__ = "host"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    hostname = Column(String(32))
    port = Column(String(32))
    ip = Column(String(32)) host_user = relationship("HostUser",
    secondary=Host_HostUser,
    backref="h") class HostUser(Base):
    """
    登录主机的用户表
    """
    __tablename__ = "host_user"
    nid = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(32)) # Base.metadata.create_all(engine) # 创建表 Session = sessionmaker(bind=engine)
    session = Session() ### 新式方法五 host_obj = session.query(Host).filter(Host.hostname=="c1").first()
    print(host_obj.host_user)

paramiko模块

python学习之路-13的更多相关文章

  1. Python学习之路13☞常用模块

    一 time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(t ...

  2. python学习之路-day2-pyth基础2

    一.        模块初识 Python的强大之处在于他有非常丰富和强大的标准库和第三方库,第三方库存放位置:site-packages sys模块简介 导入模块 import sys 3 sys模 ...

  3. Python学习之路-Day2-Python基础2

    Python学习之路第二天 学习内容: 1.模块初识 2.pyc是什么 3.python数据类型 4.数据运算 5.bytes/str之别 6.列表 7.元组 8.字典 9.字符串常用操作 1.模块初 ...

  4. Python学习之路【第一篇】-Python简介和基础入门

    1.Python简介 1.1 Python是什么 相信混迹IT界的很多朋友都知道,Python是近年来最火的一个热点,没有之一.从性质上来讲它和我们熟知的C.java.php等没有什么本质的区别,也是 ...

  5. python学习之路------你想要的都在这里了

    python学习之路------你想要的都在这里了 (根据自己的学习进度后期不断更新哟!!!) 一.python基础 1.python基础--python基本知识.七大数据类型等 2.python基础 ...

  6. Python学习之路-Day2-Python基础3

    Python学习之路第三天 学习内容: 1.文件操作 2.字符转编码操作 3.函数介绍 4.递归 5.函数式编程 1.文件操作 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个 ...

  7. Python学习之路-Day1-Python基础

    学习python的过程: 在茫茫的编程语言中我选择了python,因为感觉python很强大,能用到很多领域.我自己也学过一些编程语言,比如:C,java,php,html,css等.但是我感觉自己都 ...

  8. python学习之路网络编程篇(第四篇)

    python学习之路网络编程篇(第四篇) 内容待补充

  9. python 学习之路开始了

    python 学习之路开始了.....记录点点滴滴....

随机推荐

  1. [Cycle.js] Read effects from the DOM: click events

    So far we only had effects that write something to the external world, we are not yet reading anythi ...

  2. EClipse开发NDK流程

    EClipse开发NDK流程(现在studio也在2.2之后支持了非常简单,只要创建项目的时候勾选c++支持就可以了)   什么情况下使用ndk,1.保护代码,java很容易反编译,c/c++反汇编比 ...

  3. IE6 浏览器常见兼容问题 大汇总(23个)

    IE6以及各个浏览器常见兼容问题 大汇总 综述:虽然说IE6在2014年4月将被停止支持,但是不得不说的是,IE6的市场并不会随着支持的停止而立刻消散下去,对于WEB前端开发工程师来说,兼容IE6 兼 ...

  4. 一致性哈希与java实现

    一致性哈希算法是分布式系统中常用的算法.比如,一个分布式的存储系统,要将数据存储到具体的节点上,如果采用普通的hash方法,将数据映射到具体的节点上,如key%N,key是数据的key,N是机器节点数 ...

  5. JSON转Model内部实现解析

    一.思路: 1.通过模型类型获得所有的属性和其类型 2.对获得的json进行处理.类型处理 3.考虑字典键值和模型属性名不一致的情况 4.添加code用于归档 5.补充JSON转字典.字典转JSON. ...

  6. with 与 debugger

    with在严格模式下是禁止使用的,而debugger是在调试模式下才有效果的,目测作者自己在用的脚本压缩工具在有dubugger语句的情况下会影响压缩结果,导致失败. with(varible)实际上 ...

  7. JAVA $ JSP

    1. java中数据类型分为两种   基本数据类型:数值型,字符型,布尔型   引用数据类型:类,接口,数组 基本数据类型所占空间固定,有别与C++,java中没有无符号数byte:1字节short: ...

  8. EasyShortcut Easyshortcut easyShortcut 简介

    关于EasyShortcut Easyshortcut easyShortcut 简介: 参考: http://chunsheng.me/EasyShortcut/

  9. Modified Kaprekar Numbers

    Link: https://www.hackerrank.com/challenges/kaprekar-numbers from __future__ import print_function d ...

  10. sqlserver中临时表、row-number、update更新自己

    SELECT * INTO #TempStudent FROM (SELECT id, ROW_NUMBER() OVER(ORDER BY id) RowNum FROM dbo.student) ...