• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Deprecation warning 질문

24.02.27 18:20 작성 24.02.28 09:49 수정 조회수 115

0

14강중에서요 밑에 파이썬 코드를 치고 실행하면 실행은 됩니다. 그러나

 

The default datetime adapter is deprecated as of Python 3.12; see

the sqlite3 documentation for suggested replacement recipes

cs.execute("INSERT INTO tbl_test(Column1, Column2) VALUES(?,?)",(today, now))

 

c:\DjangoInflearn\myproject\sqlite3_008.py:53: DeprecationWarning: The default date converter is deprecated as of Python 3.12; see the sqlite3 documentation for suggested replacement recipes

row = cs.fetchone()

c:\DjangoInflearn\myproject\sqlite3_008.py:53: DeprecationWarning: The default timestamp converter is deprecated as of Python 3.12; see the sqlite3 documentation for suggested replacement recipes

row = cs.fetchone()

 

이렇게 Deprecation 경고가 나옵니다. sqlite3공식문서를 통해 확인하라고 하지만 영어가 부족하여 찾기가 어렵고 찾아도 맞는지 모르니... 어떻게 입력 해야 하는지 모르겠습니다.

import sqlite3              
import datetime 

# DB 및 커서 객체생성
dbConn = sqlite3.connect(":memory:", detect_types = sqlite3.PARSE_DECLTYPES)
cs = dbConn.cursor()

#테이블 생성
cs.execute("CREATE TABLE IF NOT EXISTS tbl_test(Column1 date, Column2 timestamp)")

# 날짜, 시간 설정(함수써서 변수에 넣고)
today= datetime.date.today()
now = datetime.datetime.now()   

# 데이터입력
cs.execute("INSERT INTO tbl_test(Column1, Column2) VALUES(?,?)",(today, now))

# 데이터 출력1
print('-'*50, '출력1')
strSQL ="SELECT*FROM tbl_test"
cs.execute(strSQL)
row = cs.fetchone()

print('오늘날짜=',row[0], "-->", type(row[0])) # str 타입감지기능 안썼으니까
print('현재시각=',row[1], "-->", type(row[1])) # str 

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.02.28

안녕하세요, 인프런 AI 인턴입니다.
정근당근님, 경고 메시지에 나타난 내용은 Python 3.12 버전부터 sqlite3 모듈에서 기본적으로 제공되던 날짜 및 시간 변환기(deprecated default date and timestamp converter)가 더 이상 사용되지 않는다는 의미입니다. 이에 따라 사용자가 직접 변환 함수를 작성하여 사용해야 합니다. 관련 내용은 sqlite3 documentation에서 참고할 수 있습니다.

간단한 해결 방법으로는 sqlite3 모듈의 register_adapterregister_converter 함수를 이용해 커스텀 날짜 및 시간 변환기를 등록하는 방법이 있습니다. 예시 코드는 다음과 같습니다.

import sqlite3
import datetime

# 커스텀 변환기(adapter)와 변환함수(converter)를 등록합니다.
def adapt_date(val):
    return val.isoformat()

def convert_date(s):
    return datetime.date.fromisoformat(s.decode('ascii'))

def adapt_datetime(val):
    return val.isoformat(' ')

def convert_datetime(s):
    return datetime.datetime.fromisoformat(s.decode('ascii'))

# 커스텀 변환기(adapter) 등록
sqlite3.register_adapter(datetime.date, adapt_date)
sqlite3.register_adapter(datetime.datetime, adapt_datetime)

# 커스텀 변환함수(converter) 등록
sqlite3.register_converter("date", convert_date)
sqlite3.register_converter("timestamp", convert_datetime)

# DB 및 커서 객체 생성 및 타입 감지 등록
dbConn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
cs = dbConn.cursor()

# 코드 나머지 부분은 예전과 동일하게 작성합니다.
# ...

위의 코드에서 adapt_date, convert_date, adapt_datetime, convert_datetime 함수들은 Python의 datetime.datedatetime.datetime 타입을 SQLite 데이터베이스가 이해할 수 있는 문자열로 변환하고, 그 반대로 데이터베이스에서 다시 Python의 날짜/시간 타입으로 변환하는 작업을 수행합니다.

이렇게 변환 함수를 등록하면 DeprecationWarning이 나타나지 않게 됩니다. 만약 추가적인 도움이 필요하시다면, 영어 문서의 이해가 어려우실 경우 문서 번역 도구나 커뮤니티의 도움을 받아 번역하여 확인해 보실 수 있습니다.