본문 바로가기

Research

SQLITE3 빠르게 시작하기 - 필요한 명령어들만 모아둠

sqlite 세팅하기

출력을 이쁘게 만들기 위한 내용

(bash) vi ~/.sqliterc
.headers ON
.mode column
.separator ROW "\n"
.nullvalue NULL
(bash) sqlite3 test.db

쉘 켜고 끄기

데이터베이스 만들기

(bash) sqlite3 test.db
(out) sqlite>

쉘에서 나가기

sqlite> .quit

데이터베이스 관리

데이터베이스 확인하기

sqlite> .databases
(out) main: /home/sh0416/test.db

테이블 관리

테이블 확인하기

sqlite> .tables
(out) 없음

테이블 만들기

sqlite> CREATE TABLE IF NOT EXISTS table1 (
  column1 INTEGER,
  column2 REAL NOT NULL,
  column3 TEXT
);
(out) 없음

테이블 스키마 확인하기

sqlite> .schema table1
(out) CREATE TABLE table1 ( column1 INTEGER, column2 REAL NOT NULL, column3 TEXT );

좀더 자세하게 테이블 스키마 확인하기

sqlite> pragma table_info('table1');
(out)
cid         name        type        notnull     dflt_value  pk
----------  ----------  ----------  ----------  ----------  ----------
0           column1     INTEGER     0           NULL        0
1           column2     REAL        1           NULL        0
2           column3     TEXT        0           NULL        0

테이블 자세하게 설정하기는 다음 글에서 적을 계획.

테이블 삭제하기

sqlite> DROP TABLE table1;
(out) 없음

레코드 관리

레코드 집어넣기

sqlite> INSERT INTO table1 (column1, column2, column3) VALUES (1, 2.4, "hi");
(out) 없음

레코드 조회하기

sqlite> SELECT * FROM table1;
(out)
column1     column2     column3
----------  ----------  ----------
1           2.4         hi

레코드 삭제하기

sqlite> DELETE FROM table1 WHERE column1 == 1;
sqlite> SELECT * FROM table1;
(out) 없음

테이블 추출

테이블 csv 파일로 변환하기

sqlite> .mode csv
sqlite> .output test.csv
sqlite> SELECT * FROM table1;
sqlite> .quit
(bash) cat test.csv
(out)
column1,column2,column3
1,2.4,hi

데이터베이스 안에 있는 전반적인 내용 확인하기

sqlite> SELECT * FROM sqlite_master;
(out)
type          name    tbl_name    rootpage    sql
------------  ------  ----------  ----------  ----------------------------------------------------------------------------
table         table1  table1      2           CREATE TABLE table1 ( column1 INTEGER, column2 REAL NOT NULL, column3 TEXT )