当前位置: 首页 > news >正文

网站建设规划报告湖南企业竞价优化服务

网站建设规划报告,湖南企业竞价优化服务,网页制作过程怎么写,服务外包下的网站开发2019独角兽企业重金招聘Python工程师标准>>> 继续上一篇SQLAlchemy的学习之旅。 多对多表的创建 表Host和表HostUser通过表HostToHostUser关联在一起 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 3…

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

继续上一篇SQLAlchemy的学习之旅。

 

多对多表的创建

 

表Host和表HostUser通过表HostToHostUser关联在一起

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

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://yli:yli@sydnagios:3306/mydb", max_overflow=5)

Base = declarative_base()

class HostToHostUser(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): # metaclass,Host.table对象

    __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=HostToHostUser, backref='h')

    host_user = relationship('HostUser', secondary=HostToHostUser.__table__, backref='h')

class HostUser(Base):

    __tablename__ = 'host_user'

    nid = Column(Integer, primary_key=True,autoincrement=True)

    username = Column(String(32))

def init_db():

    Base.metadata.create_all(engine)

#

# def drop_db():

#     Base.metadata.drop_all(engine)

init_db()

Session = sessionmaker(bind=engine)

session = Session()

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(hostname='c4',port='22',ip='1.1.1.4'),

    Host(hostname='c5',port='22',ip='1.1.1.5'),

])

session.commit()

session.add_all([

    HostUser(username='root'),

    HostUser(username='db'),

    HostUser(username='nb'),

    HostUser(username='sb'),

])

session.commit()

session.add_all([

    HostToHostUser(host_id=1,host_user_id=1),

    HostToHostUser(host_id=1,host_user_id=2),

    HostToHostUser(host_id=1,host_user_id=3),

    HostToHostUser(host_id=2,host_user_id=2),

    HostToHostUser(host_id=2,host_user_id=4),

    HostToHostUser(host_id=2,host_user_id=3),

])

session.commit()

 

结果如下

wKiom1gug5PTGEhPAABJRS1K_EE945.png

wKioL1gug5STfOkHAABW4OjOmTs833.png

wKiom1gug5TQ-D-sAAA-JyD-sQ8039.png

 

例1. 获取主机1的所有用户,原理和1对多的一样,通过relationship快速定位到对应的表

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

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://yli:yli@sydnagios:3306/mydb", max_overflow=5)

Base = declarative_base()

 

class HostToHostUser(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')

     

class Host(Base): # metaclass,Host.table对象

    __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))

     

#def init_db():

#    Base.metadata.create_all(engine)

#

# def drop_db():

#     Base.metadata.drop_all(engine)

#init_db()

 

Session = sessionmaker(bind=engine)

session = Session()

 

host_obj = session.query(Host).filter(Host.hostname=='c1').first()

print(host_obj.nid)

print(host_obj.hostname)

# 第三表对应的对象(逆向查询)

print(host_obj.h)

# 循环获取的第三表对应的对象

for item in host_obj.h:

    print(item.host_user,item.host_user.nid,item.host_user.username)

    --------------

"C:\Program Files\Python3\python.exe" "C:/Users/yli/Documents/Tencent Files/38144205/FileRecv/s13课上代码/s13课上代码/s13day13课上代码/s13day13_课上代码/ORM—de,p.py"

1

c1

[<__main__.HostToHostUser object at 0x000002678C0A3CC0>, <__main__.HostToHostUser object at 0x000002678C0B53C8>, <__main__.HostToHostUser object at 0x000002678C0B5438>]

<__main__.HostUser object at 0x000002678C0B57481 root

<__main__.HostUser object at 0x000002678C0B59082 db

<__main__.HostUser object at 0x000002678C0B5AC83 nb

 

Process finished with exit code 0

 

 

例2,另外一种方式关联多表可以在group表上面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

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://yli:yli@sydnagios:3306/mydb", max_overflow=5)

Base = declarative_base()

 

class HostToHostUser(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')

class Host(Base): # metaclass,Host.table对象

    __tablename__ = 'host'

    nid = Column(Integer, primary_key=True,autoincrement=True)

    hostname = Column(String(32))

    port = Column(String(32))

    ip = Column(String(32))

     

    #这次是在Host表上做关联,指定通过HostToHostUser表给HostUser关联起来

    host_user = relationship('HostUser', secondary=HostToHostUser.__table__, backref='h')

class HostUser(Base):

    __tablename__ = 'host_user'

    nid = Column(Integer, primary_key=True,autoincrement=True)

    username = Column(String(32))

    # host = relationship("Host", backref='h')

    # host_user = relationship("HostUser", backref='u')

def init_db():

    Base.metadata.create_all(engine)

#

def drop_db():

    Base.metadata.drop_all(engine)

# init_db()

# # drop_db()

Session = sessionmaker(bind=engine)

session = Session()

 

 

host_obj = session.query(Host).filter(Host.hostname == 'c1').first()

print(host_obj.host_user)

for item in host_obj.host_user:

    print(item.username)

     

     

------------

[<__main__.HostUser object at 0x000001D422BCEBA8>, <__main__.HostUser object at 0x000001D422BCE550>, <__main__.HostUser object at 0x000001D422BCE630>]

root

db

nb

 

上面两张方式,个人觉得第一种更容易理解,第二种写代码更省事

 

 

登录乐搏学院官网http://www.learnbo.com/

或关注我们的官方微博微信,还有更多惊喜哦~

 

本文出自 “麻婆豆腐” 博客,请务必保留此出处http://beanxyz.blog.51cto.com/5570417/1874217

转载于:https://my.oschina.net/learnbo/blog/870733

http://www.lbrq.cn/news/2385955.html

相关文章:

  • 哪个公司可以专门做网站今天今日头条新闻
  • 香港特别行政区土地面积seo怎么优化
  • 网站开发验收方案新闻内容摘抄
  • 张家界酷网科技网站建设网站转让出售
  • 宝安做棋牌网站建设哪家技术好官网seo是什么
  • 动漫设计软件厦门网站优化
  • 大淘客网站怎样做百度推广长沙seo优化推广公司
  • 网站改了关键词百度关键词优化软件
  • 服装网站建设策划书地推公司
  • wordpress 留言簿seo具体怎么优化
  • 免费外国黄色网站sem是什么显微镜
  • 北京手机网站建设公司百度seo排名360
  • 做羞羞的专门网站重庆seo黄智
  • 中国建设银行嵊州市支行网站搜索引擎广告形式有
  • 优化企业网站模板企业查询网
  • 小米路由器做网站服务器吗seo搜索引擎优化实训总结
  • 银川如何做百度的网站自助建站系统代理
  • 会同县政府网站建设2023广东又开始疫情了吗
  • 设计作品靖江seo要多少钱
  • 公司电子商务网站建设规划方案永久免费用的在线客服系统
  • 台州招聘网站建设河北seo公司
  • wordpress如何实现北京seo排名公司
  • 微网站用什么做5g网络优化培训
  • 聊城住房建设局网站手机做网页的软件
  • 郑州鹏之信网站建设关键词检测工具
  • 苏州个人网站制作公司廊坊seo整站优化
  • seo优化网站网络游戏推广平台
  • 网站建设如何选择百度浏览器极速版
  • 网站备案多长时间来完成百度第三季度财报2022
  • 响应式网站设计建设制作软文形式推广产品
  • vscode 一直连不上远程,网络是通的,ssh 也能直接登录远程
  • FLTK UI窗口关闭时延时卡顿问题全流程分析与优化实战
  • 51c视觉~合集13
  • 从AWS MySQL数据库下载备份到S3的完整解决方案
  • ROS1/Linux——Launch文件使用
  • 深入理解-Java-线程池:原理、动态调整与监控实践