SQLiteを使ってみた
SQLiteは、4年ほど前だったかAndroidがリリースされた直後にアーキテクチャを調べていた際に使用した程度。
MySQLを使おうか、SQLiteを使おうか迷ったが、今のところ大したことはやらない予定なので軽量な方を選択した。
・データベースの作成
コマンドプロンプトで、下記をうつ。
sqlite3.exe データベース名
実行例。
C:\dev\SQLite>sqlite3.exe test.db SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";"
・データベースの確認
sqlite> .databases seq name file --- --------------- --------------------------------------------------------- 0 main C:\dev\SQLite\test.db sqlite>
・テーブルの作成および確認
sqlite> create table testTBL(id, name); sqlite> .table testTBL sqlite>
注)「sqlite_」で始まるテーブル名はSQLite自体が使用するため使うことはできない。
sqlite> create table sqlite_testTBL(id, name); Error: object name reserved for internal use: sqlite_testTBL
・テーブル作成時にカラムの型を指定する場合は次のように記述する。
sqlite> create table test1TBL(id integer, name text); sqlite> .table test1TBL testTBL sqlite>
・テーブルの構造を調べる。
1.scheamが保持するテーブル全ての構造を調べる。
sqlite> .schema CREATE TABLE testTBL(id, name); CREATE TABLE test1TBL(id integer, name text);
2.特定のテーブルの構造を調べる。
sqlite> .tables test1TBL testTBL sqlite> .schema test1TBL CREATE TABLE test1TBL(id integer, name text); sqlite>
・テーブルへデータを挿入する
sqlite> insert into test1TBL values(1,'endo'); sqlite> select * from test1TBL; 1|endo sqlite>
・他のテーブルから検索したデータを挿入する
sqlite> select * from testTBL; 1|endo 2|sato 3|suzuki 4|kaji sqlite> select * from test1TBL; sqlite> insert into test1TBL select * from testTBL; sqlite> select * from test1TBL; 1|endo 2|sato 3|suzuki 4|kaji
・テーブルのデータを変更する
sqlite> update test1TBL set id=0, name='sato'; sqlite> select * from test1TBL; 0|sato sqlite>
・テーブルからデータを削除する
sqlite> delete from test1TBL where id=0; sqlite> select * from test1TBL; sqlite>