[SQLite] 시작

리눅스에서 설치한다. 의존 패키지는 거의 없다시피 하다.

apt install sqlite3

기본적으로 데이터베이스는 다음과 같은 식으로 만들 수 있다. weather 이름을 가진 DB를 만들고 싶으면 다음과 같이 한다.

# sqlite3 weather.db

빈 테이블을 만드는 방법이 딱히 있는게 아닌 것 같다. 일반적으로 이렇게 하면 빈 컬럼이 생성된다.

create table weather("");

컬럼을 추가한다. 속성은 INTEGER, TEXT, BLOB, NUMERIC, REAL 로 5개 뿐이다. Date 등도 텍스트, 실수, 소수 등으로 처리해야 한다.

alter table weather add column frct integer;
alter table weather add column tmp integer;
alter table weather add column pop integer;
alter table weather add column pcp integer;
alter table weather add column wsd real;
alter table weather add column pty integer;
alter table weather add column sky integer;

파이썬에서는 다음과 같이 사용한다. Auto-commit 기능은 isolation_level = None을 이용하면 사용할 수 있다.

import sqlite3

con = sqlite3.connect('weather.db',  isolation_level=None)
cur = con.cursor()

con.execute('INSERT INTO weather (frct) VALUES(:frct)', {'frct': 'frct'})