■ 날짜/시간을 처리하는 방법을 보여준다.
▶ 테스트 테이블 (SQL)
1 2 3 4 5 6 7 8 9 |
CREATE TABLE item ( item_id INTEGER NOT NULL PRIMARY KEY, item_name VARCHAR (100) NOT NULL, purchase_time TIMESTAMP NOT NULL, price INTEGER NOT NULL ); |
▶ 예제 코드 (PY)
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 |
import datetime import psycopg2 connection = psycopg2.connect(host = "192.168.29.118", port = "5432", database = "testdb", user = "testuser1", password = "test1234") cursor = connection.cursor() cursor.execute("DELETE FROM item;") cursor.execute( "INSERT INTO item (item_id, item_name, purchase_time, price) VALUES (%s, %s, %s, %s)", (12, "Keyboard", datetime.datetime.now(), 150) ) connection.commit() cursor.execute("SELECT purchase_time from item where item_id = 12") purchaseTime = cursor.fetchone() print("항목 구매 일자 :", purchaseTime[0].date()) print("항목 구매 시간 :", purchaseTime[0].time()) cursor.close() connection.close() """ 항목 구매 일자 : 2024-06-01 항목 구매 시간 : 00:58:33.167324 """ |
▶ requirements.txt
1 2 3 |
psycopg2-binary==2.9.9 |