■ Session 클래스의 delete 메소드를 사용해 테이블 행을 삭제한다.
※ test.db 파일의 event 테이블에 데이터가 있는 것을 전제로 테스트해야 한다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
from sqlmodel import SQLModel, Session, JSON, Field, Column, create_engine, select from typing import List class Event(SQLModel, table = True): id : int | None = Field(default = None, primary_key = True) title : str image : str description : str location : str tagList : List[str] = Field(sa_column = Column(JSON)) databaseFilePath = "test.db" databaseURL = f"sqlite:///{databaseFilePath}" connectionArgumentDictionary = {"check_same_thread" : False} engine = create_engine(databaseURL, echo = False, connect_args = connectionArgumentDictionary) SQLModel.metadata.create_all(engine) with Session(engine) as session: sql = select(Event) eventList = session.exec(sql).all() for event in eventList: print(event.id, event.title) print() eventCount = len(eventList) event = eventList[eventCount - 1] session.delete(event) session.commit() eventList = session.exec(sql).all() for event in eventList: print(event.id, event.title) |