def create_stu_many(): # 实现创建多个功能 names = ['纳兰', '性德', '容若', '太白', '牧之', '青莲', '武斗', '南山', '一笑', '殺'] stu_list = [] for name in names: stu = Students() stu.s_name = name # 避免多次IO操作 # stu.save() stu_list.append(stu) db.session.add_all(stu_list) db.session.commit() return '创建多个学生信息成功'
def create_stu_many(): # 实现创建多个功能 names = ['贪狼', '释天', '无相', '青冥', '太上', '无为', '北斗', '七殇', '三才', '布衣'] stu_list = [] for name in names: stu = Students() stu.s_name = name # 避免多次IO操作 # stu.save() stu_list.append(stu) db.session.add_all(stu_list) db.session.commit() return '创建多个学生信息成功'
def create_stu_all(): # 批量创建,add_all() names = ['小王', '老王', '厂长', '李哥', '小花'] stu_list = [] for name in names: stu = Students() stu.s_name = name # stu.save() # db.session.add(stu) stu_list.append(stu) db.session.add_all(stu_list) db.session.commit() return '批量创建学生信息成功'
def create_stus_gra(): names = ['老王', '55开', 'uzi', '大哥', '麻辣香锅', 'god', 'wawa'] stu_list = [] i = 1 for name in names: stu = Students() stu.s_name = name stu.s_g = i % 5 + 1 i += 1 stu_list.append(stu) db.session.add_all(stu_list) db.session.commit() return '一对多批量学生添加成功'
def create_stu_gra(): stu = Students() stu.s_name = '李哥' stu.s_age = 27 stu.s_g = 3 stu.save() return '一对多一个学生添加成功'
def create_students(self): payload = [] count = 0 try: students_to_create = self.requests.get("students", None) with DBConnection() as session: for student in students_to_create: _id = str(uuid.uuid4()) try: new_user = Students( student_id=_id, name=student['name'], usn=student['usn'].lower(), email=student['email'], password=pbkdf2_sha256.encrypt(student['password'], rounds=1200, salt_size=32), branch_id=student['branch_id'], sem=student['sem'], year=student['year'], created_by=_id, created_on=datetime.now(), modified_by=_id, modified_on=datetime.now(), ) session.add(new_user) session.commit() payload.append({ "student_id": _id, "message": "Student added successfully." }) count += 1 except SQLAlchemyError as e: print(e) payload.append({ "student_id": _id, "message": str(e._message).split(": ")[1].split('\\')[0] }) session.rollback() except Exception as e: print(e) raise e finally: return payload, str(count) + " student created."
def create_stu(): # 实现创建功能 stu = Students() stu.s_name = '小明1' stu.save() return '创建学生信息成功'
def create_stu(): # 实现学生对象的创建 stu = Students() stu.s_name = '小明' stu.save() return '创建学生信息成功'