Пример #1
0
 def default(self, *vpath, **params):
     current_session = Session()
     try:
         method = getattr(self, "handle_" + cherrypy.request.method, None)
         if not method:
             methods = [x.replace("handle_", "")
                for x in dir(self) if x.startswith("handle_")]
             cherrypy.response.headers["Allow"] = ",".join(methods)
             raise cherrypy.HTTPError(405, "Method not implemented.")
         return method(current_session, *vpath, **params)
     except:
         self.log_exception("restresource.default() exception")
         return "There was an error attempting to service the request"
     finally:
         current_session.close()
Пример #2
0
# for dep in query11:
#     print(dep.dep_id, dep.dep_name)
#############################
query12 = session.query(Departments).count()
# print(query12)  # 统计departments表的行数
#############################
query13 = session.query(Employees.emp_name, Departments.dep_name)\
    .join(Departments)  # 员工表和部门表有主外键约束,join后,可以得到正确结果
# for emp_name, dep_name in query13:
#     print(emp_name, dep_name)
#############################
query14 = session.query(Departments.dep_name, Employees.emp_name)\
    .join(Employees)
# for dep_name, emp_name in query14:
#     print(emp_name, dep_name)
#############################
# 找到实例,重新赋值,即可实现更新操作
query15 = session.query(Departments)\
    .filter(Departments.dep_name=='人事部')
# print(query15.all())  # 返回的是长度为1的列表
# print(query15.one())  # 返回的是实例
# hr = query15.one()
# hr.dep_name = '人力资源部'
#############################
query16 = session.query(Departments).filter(Departments.dep_id==6)
ui = query16.one()
session.delete(ui)
#############################
session.commit()
session.close()
Пример #3
0
# print(qset8)  # qset8是SQL语句
# print(qset8.all())   # all方法返回列表
# print(qset8.first())  # first返回all中的第一项
#######################################
# qset9 = session.query(Departments.dep_id, Departments.dep_name)\
#     .filter(Departments.dep_id==20)
# # one方法要求查询的结果只有一项,0或多项都报错
# print(qset9.one())
#######################################
# 多表查询,默认情况下sqlalchemy会自动根据主外键约束找到对应关系
# 查询的时候参数先写Employees,join要写Departments,反之亦然
# qset10 = session.query(Employees.emp_name, Departments.dep_name)\
#     .join(Departments)
# print(qset10.all())
#######################################
# 修改,只要将实例重新赋值
# qset11 = session.query(Departments).filter(Departments.dep_name=='人事部')
# hr = qset11.one()
# hr.dep_name = '人力资源部'
#######################################
# 删除,先找到实例再删除
qset12 = session.query(Departments).filter(Departments.dep_id==6)
sales = qset12.one()
session.delete(sales)



#######################################
session.commit()   # 确认
session.close()    # 关闭会诂连接