테이블에 데이터 삽입

Python으로 배우는 데이터베이스 입문

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

테이블에 데이터 추가

  • insert() 문 사용 완료
  • insert()는 데이터를 넣을 테이블을 인수로 받음
  • 넣을 값은 values 절에 컬럼=값 쌍으로 지정
  • 행을 반환하지 않으므로 fetch 메서드 불필요
Python으로 배우는 데이터베이스 입문

단일 행 삽입

from sqlalchemy import insert
stmt = insert(employees).values(id=1,name='Jason', 
          salary=1.00, active=True)

result_proxy = connection.execute(stmt) print(result_proxy.rowcount)
1
Python으로 배우는 데이터베이스 입문

다중 행 삽입

  • 값 없이 insert 문 생성
  • 삽입할 각 행의 values 절을 나타내는 딕셔너리 목록 작성
  • 연결의 execute에 문과 값 목록을 함께 전달
Python으로 배우는 데이터베이스 입문

다중 행 삽입

stmt = insert(employees)

values_list = [{'id': 2, 'name': 'Rebecca', 'salary': 2.00, 'active': True}, {'id': 3, 'name': 'Bob', 'salary': 0.00, 'active': False}]
result_proxy = connection.execute(stmt, values_list)
print(result_proxy.rowcount)
2
Python으로 배우는 데이터베이스 입문

연습해 봅시다!

Python으로 배우는 데이터베이스 입문

Preparing Video For Download...