目次
SQLite3をさわったメモ
SQLiteについて
- ユーザーの概念が無い
- データ型の管理を行わない
- 1データベースが1ファイルである
- ↓参考リンク
- SQLite - Wikipedia
- SQLiteを使うべき10の理由と5つのデメリット - CPA-LABテクニカル
- sqlite 特徴 - Google 検索
SQLiteに接続する
sqlite3.exeをダウンロードしてくるか /android-sdk-windows/tools/sqlite3.exe などを利用する。 sqlite3.exeのあるフォルダへ移動して実行する。コマンドライン引数に開くデータベース名を入力する。
> sqlite3.exe testdb.sqlite3
SQLite version 3.7.6
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
これで testdb.sqlite3 作成/接続できる。拡張子は何でもOK。
コマンドを打ち込んでみる
基本はSQL…といってもわずかな触りの部分しか分からないけど…orz
sqlite> .help
.backup ?DB? FILE Backup DB (default "main") to FILE
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices ?TABLE? Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY? Load an extension library
.log FILE|off Turn logging on or off. FILE can be stderr/stdout
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.restore ?DB? FILE Restore content of DB (default "main") from FILE
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.stats ON|OFF Turn stats on or off
.tables ?TABLE? List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.timeout MS Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off
sqlite> DROP TABLE IF EXISTS FOO;
sqlite> CREATE TABLE FOO (
...> ID INTEGER PRIMARY KEY,
...> BAR
...> );
sqlite> INSERT INTO FOO(BAR) VALUES("こんにちワン");
sqlite> INSERT INTO FOO(BAR) VALUES("こんばんワニ");
sqlite> INSERT INTO FOO(BAR) VALUES("おはよウナギ");
sqlite> SELECT * FROM FOO;
1|こんにちワン
2|こんばんワニ
3|おはよウナギ
sqlite> .mode column
sqlite> SELECT * FROM FOO;
1 こんにちワン
2 こんばんワニ
3 おはよウナギ
sqlite> .width 2
sqlite> SELECT * FROM FOO;
1 こんにちワン
2 こんばんワニ
3 おはよウナギ
sqlite> UPDATE FOO SET BAR = "さようなライオン" WHERE ID = 3;
sqlite> SELECT * FROM FOO;
1 こんにちワン
2 こんばんワニ
3 さようなライオン
sqlite> CREATE TABLE HOGE(A,B,C);
sqlite> INSERT INTO HOGE VALUES(111,100,1);
sqlite> INSERT INTO HOGE VALUES(111,160,2);
sqlite> INSERT INTO HOGE VALUES(111,109,3);
sqlite> INSERT INTO HOGE VALUES(222,140,1);
sqlite> INSERT INTO HOGE VALUES(222,106,2);
sqlite> INSERT INTO HOGE VALUES(333,777,1);
sqlite> .header ON
sqlite> SELECT * FROM HOGE;
A B C
---------- ---------- ----------
111 100 1
111 160 2
111 109 3
222 140 1
222 106 2
333 777 1
sqlite> SELECT * FROM FOO;
ID BAR
---------- ----------
1 おはよウナギ
2 こんばんワニ
3 さようなライオン
sqlite> SELECT A,SUM(A),SUM(B),SUM(C) FROM HOGE GROUP BY A;
A SUM(A) SUM(B) SUM(C)
---------- ---------- ---------- ----------
111 333 369 6
222 444 246 3
333 333 777 1
sqlite> SELECT A,TOTAL(A),AVG(B),MAX(C) FROM HOGE WHERE A < 300 GROUP BY A;
A TOTAL(A) AVG(B) MAX(C)
---------- ---------- ---------- ----------
111 333.0 123.0 3
222 444.0 123.0 2
sqlite> SELECT A,BAR FROM HOGE INNER JOIN FOO ON HOGE.C = FOO.ID;
A BAR
---------- ----------
111 おはよウナギ
111 こんばんワニ
111 さようなライオン
222 おはよウナギ
222 こんばんワニ
333 おはよウナギ
sqlite> SELECT A,BAR FROM HOGE INNER JOIN FOO ON HOGE.C = FOO.ID WHERE A = 222;
A BAR
---------- ----------
222 おはよウナギ
222 こんばんワニ
sqlite> SELECT A,GROUP_CONCAT(BAR," & ") FROM HOGE INNER JOIN FOO ON HOGE.C = FOO.ID WHERE A < 300 GROUP BY A;
A GROUP_CONCAT(BAR," & ")
---------- ----------------------------------
111 おはよウナギ & こんばんワニ & さようなライオン
222 おはよウナギ & こんばんワニ
sqlite> UPDATE FOO SET BAR = "ぽぽぽぽーん";
sqlite> SELECT BAR FROM FOO;
BAR
------------
ぽぽぽぽーん
ぽぽぽぽーん
ぽぽぽぽーん
sqlite> DELETE FROM FOO;
sqlite> SELECT * FROM FOO;
sqlite> .exit
SUMとかGROUP_CONCATとかそういうものはSQLite関数で調べ