码迷,mamicode.com
首页 > 编程语言 > 详细

python学习之路

时间:2018-02-12 18:43:04      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:列表、字典、集合   def   data   pymysql   red   存在   play   pos   rem   

老男孩教育Python自动化2.0课程课件目录   http://www.cnblogs.com/alex3714/articles/5885096.html

Day1 - Python基础1  介绍、基本语法、流程控制


第十天---twisted---redis---rabbitmq

windows下redis安装  https://www.cnblogs.com/jaign/articles/7920588.html

windows下rabbitmq安装  http://blog.csdn.net/hzw19920329/article/details/53156015from sqlalchemy import create_engine

http://www.cnblogs.com/alex3714/articles/5248247.html

第十一天 sqlalchemy

一对多

技术分享图片
 1 from sqlalchemy.ext.declarative import declarative_base
 2 from sqlalchemy import Column, Integer, String, ForeignKey,func,Table
 3 from  sqlalchemy.orm import sessionmaker, relationship
 4 Base = declarative_base() #生成一个SqlORM 基类
 5 mima = !Saiyun12#$
 6 Host2Group = Table(host_2_group,Base.metadata,
 7                    Column(host_id,ForeignKey(hosts.id),primary_key=True),
 8                    Column(goup_id,ForeignKey(group.id),primary_key=True),
 9                    )
10 engine = create_engine("mysql+pymysql://root:%s@localhost/testdb" % mima,encoding=utf-8, echo=True)
11 class Host(Base):
12     __tablename__ = hosts
13     id = Column(Integer,primary_key=True,autoincrement=True)
14     hostname = Column(String(64),unique=True,nullable=False)
15     ip_addr = Column(String(128),unique=True,nullable=False)
16     port = Column(Integer,default=22)
17     group_id = Column(Integer,ForeignKey(group.id))
18     group = relationship("Group",backref=host_list)
19 class Group(Base):
20     __tablename__ = group
21     id = Column(Integer,primary_key=True)
22     name = Column(String(64),unique=True,nullable=False)
23 Base.metadata.create_all(engine) #创建所有表结构
24 if __name__ == __main__:
25     SessionCls = sessionmaker(bind=engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
26     session = SessionCls()
27     objs = session.query(Host,func.count(Group.name)).join(Host.group).group_by(Group.name).all()  #join操作
28     print("---->",objs)
29     session.commit()  # 提交
30 ‘‘‘ obj对应的原声sql
31 SELECT hosts.id AS hosts_id, hosts.hostname AS hosts_hostname, hosts.ip_addr AS hosts_ip_addr, hosts.port AS hosts_port, 
32 hosts.group_id AS hosts_group_id, count(`group`.name) AS count_1 
33 FROM hosts INNER JOIN `group` ON `group`.id = hosts.group_id GROUP BY `group`.name
34  ####select * from hosts right join testdb.group on hosts.group_id = testdb.group.id
35 ‘‘‘
View Code

多对多

技术分享图片
 1 from sqlalchemy import create_engine
 2 from sqlalchemy.ext.declarative import declarative_base
 3 from sqlalchemy import Column, Integer, String, ForeignKey,func,Table
 4 from  sqlalchemy.orm import sessionmaker, relationship
 5 Base = declarative_base() #生成一个SqlORM 基类
 6 mima = !Saiyun12#$
 7 Host2Group = Table(host_2_group,Base.metadata,
 8                    Column(host_id,ForeignKey(hosts.id),primary_key=True),
 9                    Column(goup_id,ForeignKey(group.id),primary_key=True),
10                    )
11 engine = create_engine("mysql+pymysql://root:%s@localhost/testdb" % mima,encoding=utf-8, echo=True)
12 class Host(Base):
13     __tablename__ = hosts
14     id = Column(Integer,primary_key=True,autoincrement=True)
15     hostname = Column(String(64),unique=True,nullable=False)
16     ip_addr = Column(String(128),unique=True,nullable=False)
17     port = Column(Integer,default=22)
18     groups = relationship(Group,secondary=Host2Group,backref=host_list)
19     # group_id = Column(Integer,ForeignKey(‘group.id‘))
20     # group = relationship("Group",backref=‘host_list‘)
21 class Group(Base):
22     __tablename__ = group
23     id = Column(Integer,primary_key=True)
24     name = Column(String(64),unique=True,nullable=False)
25 Base.metadata.create_all(engine) #创建所有表结构
26 if __name__ == __main__:
27     SessionCls = sessionmaker(bind=engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
28     session = SessionCls()
29     # g1= Group(name=‘g1‘)
30     # g2 = Group(name=‘g2‘)
31     # g3 = Group(name=‘g3‘)
32     # g4 = Group(name=‘g4‘)
33     # session.add_all([g1,g2,g3,g4])
34     # h1 = Host(hostname=‘h1‘,ip_addr=‘192.168.1.56‘,port=1000)
35     # h2 = Host(hostname=‘h2‘, ip_addr=‘192.168.1.57‘, port=1002)
36     # h3 = Host(hostname=‘h3‘, ip_addr=‘192.168.1.58‘, port=1003)
37     # session.add_all([h1,h2,h3])
38     groups=session.query(Group).all()
39     h1 = session.query(Host).filter(Host.hostname==h1).first()
40     h1.groups = groups  #为已经存在的host和group表创建多对多关系
41 
42     # objs = session.query(Host,func.count(Group.name)).join(Host.group).group_by(Group.name).all()  #join操作
43     # print("---->",objs)
44     session.commit()  # 提交
View Code
 

nihaohelloworld

xiao

feng

python学习之路

标签:列表、字典、集合   def   data   pymysql   red   存在   play   pos   rem   

原文地址:https://www.cnblogs.com/xiaofeng0510/p/8438884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!