8.11 数据库ORM(5)

2018-8-11 20:43:52

昨天从俺弟家回来了.

和俺弟聊天发现,他一直停留在自己目前的圈子,自己觉得很牛逼,比别人高人一等,,

读书无用论,,可以用 幸存者偏激理论 大概就是这个 可以否决,,

越努力,越幸运!

每个人选择不同,追求的东西不同!

ORM参考  https://www.cnblogs.com/wupeiqi/articles/5713330.html

 

 

 

明天看数据库的最后一节 然后进入前端知识,,然后可以愉快的Django啦!! 

睡觉睡觉!!2018-8-11 21:38:27  cf已经不大好完了,多花点时间看书!!!! 要把百年孤独看完,和苏东坡传!!!

  1 from sqlalchemy.ext.declarative import declarative_base
  2 from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index,CHAR,VARCHAR
  3 from sqlalchemy.orm import sessionmaker, relationship
  4 from sqlalchemy import create_engine
  5 
  6 Base = declarative_base()
  7 
  8 
  9 # 创建单表
 10 """
 11 1   白金
 12 2   黑金
 13 obj.xx ==> [obj,obj...]
 14 """
 15 class UserType(Base):
 16     __tablename__ = 'usertype'
 17     id = Column(Integer, primary_key=True, autoincrement=True)
 18     title = Column(VARCHAR(32), nullable=True, index=True)
 19 
 20 """
 21 1   方少伟   1
 22 2   成套     1
 23 3   小白     2
 24 # 正向
 25 ut = relationship(backref='xx')
 26 obj.ut ==> 1   白金
 27 """
 28 class Users(Base):
 29     __tablename__ = 'users'
 30     id = Column(Integer, primary_key=True, autoincrement=True)
 31     name = Column(VARCHAR(32), nullable=True, index=True)
 32     email = Column(VARCHAR(16), unique=True)
 33     user_type_id = Column(Integer,ForeignKey("usertype.id"))
 34 
 35     user_type = relationship("UserType",backref='xxoo')
 36     # __table_args__ = (
 37     #     UniqueConstraint('id', 'name', name='uix_id_name'),
 38     #     Index('ix_n_ex','name', 'email',),
 39     # )
 40 
 41 
 42 def create_db():
 43     engine = create_engine("mysql+pymysql://root:@127.0.0.1:3306/s4day62db?charset=utf8", max_overflow=5)
 44     Base.metadata.create_all(engine)
 45 
 46 def drop_db():
 47     engine = create_engine("mysql+pymysql://root:@127.0.0.1:3306/s4day62db?charset=utf8", max_overflow=5)
 48     Base.metadata.drop_all(engine)
 49 
 50 engine = create_engine("mysql+pymysql://root:@127.0.0.1:3306/s4day62db?charset=utf8", max_overflow=5)
 51 # 取一个连接 来连接用户
 52 Session = sessionmaker(bind=engine)
 53 session = Session()
 54 
 55 # 类 -> 表
 56 # 对象 -> 行
 57 # ###### 增加 ######
 58 #
 59 # obj1 = UserType(title='普通用户')
 60 # session.add(obj1)
 61 
 62 # objs =[
 63 #   UserType(title='超级用户'),
 64 #   UserType(title='白金用户'),
 65 #   UserType(title='黑金用户'),
 66 # ]
 67 # session.add_all(objs)
 68 
 69 # ###### 查 ######
 70 # print(session.query(UserType))
 71 # user_type_list = session.query(UserType).all()
 72 # for row in user_type_list:
 73 #     print(row.id,row.title)
 74 
 75 # select xxx  UserType where
 76 # user_type_list = session.query(UserType.id,UserType.title).filter(UserType.id > 2)
 77 # for row in user_type_list:
 78 #     print(row.id,row.title)
 79 
 80 # 分组,排序,连表,通配符,子查询,limit,union,where,原生SQL、
 81 # ret = session.query(Users, UserType)
 82 # select * from user,usertype;
 83 #
 84 # ret = session.query(Users, UserType).filter(Users.usertype_id==UserType.id)
 85 # select * from user,usertype whre user.usertype_id = usertype.id
 86 
 87 # result = session.query(Users).join(UserType)
 88 # print(result)
 89 
 90 # result = session.query(Users).join(UserType,isouter=True)
 91 # print(result)
 92 
 93 
 94 #
 95 
 96 # 1.
 97 # select * from b where id in (select id from tb2)
 98 
 99 # 加上  .subquery()   设置为子查询
100 
101 # 2 select * from (select * from tb) as B
102 # q1 = session.query(UserType).filter(UserType.id > 0).subquery()
103 # result = session.query(q1).all()
104 # print(result)
105 
106 # 3
107 # select
108 #   id ,
109 #   (select * from users where users.user_type_id=usertype.id)
110 # from usertype;
111 
112 
113 # session.query(UserType,session.query(Users).filter(Users.id == 1).subquery())
114 # session.query(UserType,Users)
115 # result = session.query(UserType.id,session.query(Users).as_scalar())
116 # print(result)
117 # result = session.query(UserType.id,session.query(Users).filter(Users.user_type_id==UserType.id).as_scalar())
118 # print(result)
119 
120 
121 
122 
123 
124 
125 # 问题1. 获取用户信息以及与其关联的用户类型名称(FK,Relationship=>正向操作)
126 # user_list = session.query(Users,UserType).join(UserType,isouter=True)
127 # print(user_list)
128 # for row in user_list:
129 #     print(row[0].id,row[0].name,row[0].email,row[0].user_type_id,row[1].title)
130 
131 # user_list = session.query(Users.name,UserType.title).join(UserType,isouter=True).all()
132 # for row in user_list:
133 #     print(row[0],row[1],row.name,row.title)
134 
135 
136 # user_list = session.query(Users)
137 # for row in user_list:
138 #     print(row.name,row.id,row.user_type.title)
139 
140 
141 # 问题2. 获取用户类型
142 # type_list = session.query(UserType)
143 # for row in type_list:
144 #     print(row.id,row.title,session.query(Users).filter(Users.user_type_id == row.id).all())
145 
146 # type_list = session.query(UserType)
147 # for row in type_list:
148 #     print(row.id,row.title,row.xxoo)
149 
150 
151 
152 
153 
154 
155 # ###### 删除 ######
156 # session.query(UserType.id,UserType.title).filter(UserType.id > 2).delete()
157 
158 # ###### 修改 ######
159 # session.query(UserType.id,UserType.title).filter(UserType.id > 0).update({"title" : "黑金"})
160 # session.query(UserType.id,UserType.title).filter(UserType.id > 0).update({UserType.title: UserType.title + "x"}, synchronize_session=False)
161 # session.query(UserType.id,UserType.title).filter(UserType.id > 0).update({"num": Users.num + 1}, synchronize_session="evaluate")
162 
163 
164 session.commit()
165 session.close()

 

 

posted @ 2018-08-11 11:30  我想喝杨枝甘露~  阅读(169)  评论(0编辑  收藏  举报