金子邦彦研究室インストールWindows の種々のソフトウェア(インストール)SQLite 3 のインストール,データベース作成,テーブル定義(Windows 上)

SQLite 3 のインストール,データベース作成,テーブル定義(Windows 上)

Windows での,SQLite 3 のインストール,起動と終了,データベースの新規作成,テーブル定義の手順をスクリーンショットで説明する. SQLite 3は,リレーショナルデータベース管理システム

SQLite 3は次の特徴を持つ.

目次

  1. あらかじめ決めておく事項
  2. ダウンロード
  3. SQLite 3 の起動と終了,ヘルプの表示,エンコーディングの確認
  4. 空のデータベースの新規作成
  5. テーブル定義

サイト内の主な SQLite 3 関連ページ

あらかじめ決めておく事項

ダウンロード

  1. ウェブページを開く

    https://www.sqlite.org/index.html

  2. ダウンロードしたいので「Latest Release」の右の「Download」をクリック.

    [image]
  3. Precompiled Binaries for Windows の下の,次の2つのファイルを選ぶ.

    [image]
  4. ダウンロードした .zip ファイルのうち sqlite-dll-win64-x64-3350500.zip を展開(解凍)

    Windows での展開(解凍)に便利な 7-Zip: 別ページ »で説明

    [image]
  5. ダウンロードした .zip ファイルのうち sqlite-tools-win32-x86-3350500.zip を展開(解凍)

    Windows での展開(解凍)に便利な 7-Zip: 別ページ »で説明

    [image]
  6. C:\sqlite3」のようなディレクトリを作る.

    展開してできたファイルを, 分かりやすいディレクトリの下に移したい.

    Windows のコマンドプロンプトを開き,次を実行

    mkdir C:\sqlite3
    

    [image]
  7. C:\sqlite3」の下に,展開(解凍)してできたファイルすべてをコピーする

    [image]
  8. Windowsシステム環境変数 Pathc:\sqlite3追加することにより,パスを通す

    Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

    次のコマンドを実行

    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\sqlite3\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    
  9. sqlite3 にパスが通っていることを確認する

    Windowsコマンドプロンプト新しく開き、次のコマンドを実行する.

    where sqlite3
    

    [image]
  10. 確認のため, Windowsコマンドプロンプトで、次のコマンドを実行する. エラーメッセージが出なければOK.
    sqlite3
    .exit
    

    [image]

SQLite 3 の起動と終了,ヘルプの表示,エンコーディングの確認

使い方の詳しい説明は https://www.sqlite.org/sqlite.html

空のデータベースの新規作成

ここでの設定

  1. SQLite を実行する.

    ※ パスが通っていないときは,パスを通すか,フルパスで実行する

    sqlite3
    

    [image]
  2. データベースの新規作成

    空のデータベースを作成したいので次のように操作.

    「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.

    .open --new C:/sqlite3/hoge.db
    

    [image]
  3. 「.exit」を実行して,SQLite 3 を終了.
    .exit
    

    [image]

テーブル定義,レコード挿入,SQL問い合わせ

  1. SQLite を実行する.

    ※ パスが通っていないときは,パスを通すか,フルパスで実行する

    sqlite3 
    

    [image]
  2. データベースの新規作成

    空のデータベースを作成したいので次のように操作.

    「--new 」を付けているので,すでにデータベースファイルが存在するときは,その中身を空にする.

    .open --new C:/sqlite3/hoge.db
    

    [image]
  3. テーブル定義

    ※ テーブル名に日本語を使うとエラーが出る場合がある.

    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 updated_at = (datetime('now', 'localtime')) where id = new.id; 
    end;
    
  4. 「.tables」を実行して,テーブルが定義できたことを確認.
    .tables 
    

    [image]
  5. SQL を用いたレコード挿入

    begin transaction;
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2023, 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, 2023, 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, 2023, 7, 27,  'kaneko',   'orange B', 1.2, 8 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2023, 7, 28,  'miyamoto',   'Apple L', 3 );
    commit;
    

    [image]
  6. SQL 問い合わせで,確認.

    select * from order_records;
    

    [image]
  7. 更新し確認表示

    begin transaction;
    update order_records set unit_price = 11.2 where id = 1;
    commit;
    select * from order_records;
    
  8. 「.tables」を実行して,テーブルが定義できたことを確認.
    .tables 
    
  9. 「.exit」を実行して,SQLite 3 を終了.
    .exit
    

    [image]

SQLite 3 の主なコマンド

pragma については https://www.sqlite.org/pragma.html