Windows での,SQLite 3 のインストール,起動と終了,データベースの新規作成,テーブル定義の手順をスクリーンショットで説明する. SQLite 3は,リレーショナルデータベース管理システム.
SQLite 3は次の特徴を持つ.
【目次】
【サイト内の主な SQLite 3 関連ページ】
このページでは, SQLite 3 コマンドラインシェルのインストールディレクトリを, C:\sqlite3と書く.
展開してできたファイルを, 分かりやすいディレクトリの下に移したい.
Windows のコマンドプロンプトを開き,次を実行
mkdir C:\sqlite3
コマンドプロンプトを管理者として実行: 別ページ »で説明
call powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\sqlite3\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
Windows のコマンドプロンプトを新しく開き、次のコマンドを実行する.
where sqlite3
sqlite3 .exit
このとき,データベース名として mydb を指定する.SQLite 3の流儀で,データベース名はファイル名になる.
※ データベース名はなんでも良いが、アルファベットのみを使うのが良い.
sqlite3 mydb
「.help」で,ヘルプが表示される.
.help
「PRAGMA encoding;」で,エンコーディングが表示される.
PRAGMA encoding;
「.exit」で終了.
.exit
使い方の詳しい説明は https://www.sqlite.org/sqlite.html
ここでの設定
※ パスが通っていないときは,パスを通すか,フルパスで実行する
sqlite3
空のデータベースを作成したいので次のように操作.
「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.
.open --new C:/sqlite3/hoge.db
.exit
※ パスが通っていないときは,パスを通すか,フルパスで実行する
sqlite3
空のデータベースを作成したいので次のように操作.
「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.
.open --new C:/sqlite3/hoge.db
※ テーブル名に日本語を使うとエラーが出る場合がある.
create table order_records ( id integer primary key not null, year integer not null CHECK ( year > 2008 ), month integer not null CHECK ( month >= 1 AND month <= 12 ), day integer not null CHECK ( day >= 1 AND day <= 31 ), customer_name text not null, product_name text not null, unit_price real not null check ( unit_price > 0 ), qty integer not null default 1 check ( qty > 0 ), created_at timestamp not null default (datetime('now', 'localtime')), updated_at timestamp not null default (datetime('now', 'localtime')), check ( ( unit_price * qty ) < 200000 ) ); create trigger order_records_update after update on order_records begin update order_records set last_updated_at = (datetime('now', 'localtime')) where id = new.id; end;
.tables
begin transaction; insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2020, 7, 26, 'kaneko', 'orange A', 1.2, 10 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2020, 7, 26, 'miyamoto', 'Apple M', 2.5, 2 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2020, 7, 27, 'kaneko', 'orange B', 1.2, 8 ); insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2020, 7, 28, 'miyamoto', 'Apple L', 3 ); commit;
select * from order_records;
begin transaction; update order_records set unit_price = 11.2 where id = 1; commit; select * from order_records;
.tables
.exit
pragma については https://www.sqlite.org/pragma.html
.separator , .import T1M.csv T1M
SQLite 3 のキャッシュサイズは,オープンしたデータベースについて,最大どれだけをメモリに保持するか.「-」が付いているときは,単位は1024バイト.
キャッシュサイズの変更.「-500」のように「-」を付けると,単位は 1024 バイト.単純に「512000」のように書くとバイト単位.
現在接続しているデータベースについてのジャーナルモード.ジャーナルは,トランザクションのロールバックのために用いるもの(SQLite 3 の独自用語ではない)
delete, truncate, persiste, memory, wal, off に設定できる.