Flask 쿼리 데이터베이스 작업

9583 단어 잔재주
조회와 동시에 얻은 데이터를 정렬, 추출 등 조작할 수 있다
제목 1.크게lt/크게는ge
밑줄을 두 번 긋는 것은django의 동작과 다르다.django는lt/gte이다
stus = Student.query.filter(Student.s_age.__gt__(16))  #   
stus = Student.query.filter(Student.s_age.__ge__(16)) #     

2. 소여lt/소여는le__
stus = Student.query.filter(Student.s_age.__lt__(16))
stus = Student.query.filter(Student.s_age.__le__(16))

3. 기호를 사용하여 비교 > <<=>=
stus = Student.query.filter(Student.s_age < 16)

4. in 에 속하는지 여부
stus = Student.query.filter(Student.s_age.in_([16, 22, 33, 44, 55, 8, 5]))

5. 모든 all 가져오기
sql = 'select * from Student;'
stus = db.session.execute(sql)
stus = Student.query.order_by('s_id')

6. 내림차순 정렬로 모든order 가져오기by
여기 all 필요 없어요.
stus = Student.query.order_by('s_id')

7. id 내림차순으로 일정 수량의 limit 획득
stus = Student.query.order_by('-s_id').limit(1)

8. 가장 나이가 많은 퍼스트 얻기
stus = Student.query.order_by('-s_age').first()

9. 데이터를 건너뛰고offset 가져오기
데이터 조회 2개 건너뛰기
stus = Student.query.order_by('-s_age').offset(2).limit(2)

10.get
하나만 획득가능, 메인 키에만 사용
stus = Student.query.get(24)

11.filter
지정한 id 데이터 가져오기
stus = Student.query.filter(Student.s_id == 24)

12.and_ or_ not_ 다중 조건 질의
stus = Student.query.filter(and_(Student.s_age == 18, Student.s_name == '  '))  # and  
    stus = Student.query.filter(or_(Student.s_age == 18, Student.s_name == '  '))  #     
    stus = Student.query.filter(not_(Student.s_age == 18))  #     

13. 다중 조건 조회
stus = Student.query.filter(Student.s_name == '  ', Student.s_age == 18)

좋은 웹페이지 즐겨찾기