老男孩教育Python自动化2.0课程课件目录 http://www.cnblogs.com/alex3714/articles/5885096.html
Day1 - Python基础1 介绍、基本语法、流程控制
Day2 - Python基础2 列表、字典、集合
Day3 - Python基础3 函数、递归、内置函数
Day4 - Python基础4 迭代器、装饰器、软件开发规范
Day5 - Python基础5 常用模块学习
Day6 - Python基础6 面向对象编程
Day7 - Python基础7 面向对象编程进阶
Day8 - Python网络编程 Socket编程
Day10 - Python协程、异步IO、redis缓存、rabbitMQ队列
html+css+js
第十天---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 ‘‘‘
多对多
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() # 提交
nihaohelloworld