def setUp(self): db.create_all() person1 = Student("Steph", "Curry") person2 = Student("Klay", "Thompson") person3 = Student("Kevin", "Durant") db.session.add_all([person1, person2, person3]) db.session.commit()
def add(): form = AddForm() if form.validate_on_submit(): name = form.name.data new_student = Student(name) new_student.save_to_db() return redirect(url_for('students.roll')) return render_template('add.html', form=form)
def initserver(): teacherData = xlrd.open_workbook('teacherList.xlsx') table = teacherData.sheets()[0] teacherName = table.col_values(0) for i in range(1, table.nrows): teacher = Teacher(name=teacherName[i]) db.session.add(teacher) courseData = xlrd.open_workbook('courseList.xlsx') table = courseData.sheets()[0] courseName = table.col_values(0) beginTime = table.col_values(1) duration = table.col_values(2) tchId = table.col_values(3) for i in range(1, table.nrows): timeRes = beginTime[i].split(',') course = Course(name=courseName[i], begin_time=time(hour=int(timeRes[0]), minute=int(timeRes[1]), second=int(timeRes[2])), duration=int(duration[i]), tch_id=tchId[i]) db.session.add(course) studentData = xlrd.open_workbook('studentList.xlsx') table = studentData.sheets()[0] stuId = table.col_values(0) studentName = table.col_values(1) studentDepartment = table.col_values(2) for i in range(1, table.nrows): student = Student(stu_id=stuId[i], name=studentName[i], department=studentDepartment[i], password='******') db.session.add(student) scData = xlrd.open_workbook('student_courseList.xlsx') table = scData.sheets()[0] stuId = table.col_values(0) courseId = table.col_values(1) for i in range(1, table.nrows): sc = Student_Course(stu_id=stuId[i], course_id=courseId[i], attendance_time=0, attendance_state=False) db.session.add(sc) db.session.commit() print("init successfully")
def test_make_student1(self): u = Student(first_name='First', last_name='Last', email='*****@*****.**', password='******') db.session.add(u) db.session.commit() self.assertEqual('First', u.first_name) self.assertEqual('Last', u.last_name) self.assertEqual('*****@*****.**', u.email) self.assertEqual('password', u.password)
def addstu(): if request.method == 'GET': grades = db.session.execute('select * from Grade') return render_template('addstu.html', grades=grades) if request.method == 'POST': s_name = request.form.get('s_name') s_sex = request.form.get('s_sex') g_id = request.form.get('g_name') newstu = Student(s_name=s_name, s_sex=s_sex, grade_id=g_id) db.session.add(newstu) db.session.commit() page = page = int(request.args.get('page', 1)) # 设置每页显示信息的数量 per_page = 4 paginate = Student.query.paginate(page, per_page, error_out=False) return render_template('student.html', paginate=paginate)
def setUpClass(cls): app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./' + TESTDB app.config['TESTING'] = True db.create_all() u = User(username='******', email='*****@*****.**') u.hash_password('sekret') sess = db.session sess.add(u) sess.commit() for n in ['Ivo', 'Tana', 'Natalija']: s = Student(name=n, user_id=u.id) sess.add(s) sess.commit() s = Student.query.get(1) date = datetime.datetime.strptime('2016-08-29', '%Y-%m-%d') sess.add(StudentClass(student_id=s.id, class_date=date)) sess.commit()
FYI, Am pasting the Student class here .. class Student: def __init__(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation def on_honor_roll(self): ## Have added new definition to the same class student in app file if self.gpa >= 3.5: return True else: return False """ student1 = Student("Dileep", "Business", 3.1, False) print(student1.name) print(student1.major) print(student1.gpa) print(student1.is_on_probation) print(student1.on_honor_roll()) # ========================== # OUTPUT: # Dileep # Business # 3.1 # False # ==========================
from app import db, Student, Excuse d = Student('D', 'S') l = Student('L', 'S') n = Student('DN', 'S') db.session.add_all([d, l, n]) db.session.commit() print(len(Student.query.all())) d = Student.query.get(1) excuse1 = Excuse('Dog ate the homework', False, 1) db.session.add(excuse1) db.session.commit() print(d.excuses.all()) excuse2 = Excuse('Overslept', True, 1) db.session.add(excuse2) db.session.commit() print(len(d.excuses.all()))
from app import db, Student, Hod, Teacher # create Student(name) sunny = Student("sunny") arpit = Student("arpit") db.session.add_all([sunny, arpit]) db.session.commit() # fetch DATABASE print(Student.query.all()) sunny = Student.query.filter_by(name='sunny').all()[0] # create hod hod1 = Hod("saurin", sunny.id) # create teachers teacher1 = Teacher("mihir", sunny.id) teacher2 = Teacher("yash", sunny.id) db.session.add_all([hod1, teacher1, teacher2]) db.session.commit() # Let's now grab rufus again after these additions sunny = Student.query.filter_by(name='sunny').first() print(sunny) # Show toys sunny.report_Teachers()
from app import db, Student # Creates all tables - transforms model classes to a database db.create_all() matt = Student('Matt', 25) # these inputs should come from UI ian = Student('Ian', 18) nihal = Student('Nihal', 18) # add in bulk db.session.add_all([matt, ian, nihal]) # or add individually # db.session.add(matt) # probably how we would want to implement # imagine we have a create new user button print(matt.id) # -> None print(ian.__repr__) # save changes to database db.session.commit() print(matt.id) # 1 # numpy => numpy datatype # custom class => class object, e.g. Student object
from app import db, User, Student, StudentClass import datetime db.drop_all() db.create_all() u = User(username='******', email='*****@*****.**') u.hash_password('test') db.session.add(u) db.session.commit() counter = 0 with open('names.txt', encoding='UTF-8') as f: for name in f: if counter > 1000: break s = Student(name=name.strip(), user_id=u.id) db.session.add(s) db.session.commit() for x in range(10): date = datetime.date(2019, 11, x + 1) klass = StudentClass(student_id=s.id, class_date=date) db.session.add(klass) counter += 1 db.session.commit()
from app import db, Student # CRUD - Create Read Update Delete ## CREATE ### # -------------------------------------------------------- ## Create a new entry into table manasi = Student('Manasi', 18) db.session.add(manasi) db.session.commit() # not technically saved until you commit ## READ ### read from Hours table to make queries on total hours, hours per user, etc. # -------------------------------------------------------- ## Read database all_students = Student.query.all() # returns all objects in table print(all_students) print('\n') ## Select by ID student_one = Student.query.get(1) print(student_one.name) # should be first person we add, 'Matt' print('\n') ## Filter by name # this produces SQL code!! students_18 = Student.query.filter_by(age=18) # if hours < num, date = date_range print(students_18.all()) # should be ian, nihal, and manasi in this order print('\n') ## UPDATE ### # -------------------------------------------------------- student = Student.query.get(2) # forgets password? forgets email? or typos? student.age = 17 # software eng watch out: catching data types e.g. 'seventeen' (Argument Testing)
from app import db, Student db.create_all() sunny = Student("Sunny", 20) db.session.add(sunny) db.session.commit() print(sunny.id)
db.drop_all() db.create_all() db.session.commit() #add sample entries from app import Teacher, Category, Student with app.app_context(): t1 = Teacher("Teacher1", "*****@*****.**", "password", "1234567890", "teacher") t1.is_admin = True t1.verified = True db.session.add(t1) t2 = Teacher("Teacher2", "*****@*****.**", "password", "0987654321", "teacher") db.session.add(t2) s1 = Student("Shafeeq K", "*****@*****.**", "password", "1234567890", "student") db.session.add(s1) s2 = Student("Ashwin Jayakumar", "*****@*****.**", "password", "1234567890", "student") db.session.add(s2) s3 = Student("Hanzal Salim", "*****@*****.**", "password", "1234567890", "student") db.session.add(s3) s4 = Student("Bharadhwaj CN", "*****@*****.**", "password", "1234567890", "student") db.session.add(s4) c = Category("General") db.session.add(c) c1 = Category("Embedded Systems") db.session.add(c1) c2 = Category("Compiler Design")
By using class we can create a data type and we can use that data type by calling that class. Using class we can essentially define your own data types. """ from app import Student """ FYI, Am pasting the Student class here .. class Student: def __init__(self, name, major, gpa, is_on_probation): self.name = name self.major = major self.gpa = gpa self.is_on_probation = is_on_probation """ student1 = Student("Dileep", "Business", 3.1, False) print(student1.name) print(student1.major) print(student1.gpa) print(student1.is_on_probation) # ======================== # OUTPUT: # Dileep # Business # 3.1 # ========================
from app import Student Student1 = Student("Jim", "Business", 3.1, False) Student2 = Student("Tom", "Art", 3.5, True) print(Student2.gpa)
from app import db, Student # create_all ravi = Student("ravi", 20) yash = Student("yash", 20) db.session.add_all([ravi, yash]) db.session.commit() # by filter puppy_sam = Student.query.filter_by(name='yash').first() # Returns list print(puppy_sam) print('\n') # upadate update = Student.query.get(1) update.name = "sun" db.session.add(update) db.session.commit() # delete delete = Student.query.get(2) db.session.delete(delete) db.session.commit() # read # by # ID id = Student.query.get(1) print(id) print('\n') # all all = Student.query.all()