金子邦彦研究室インストールWindows の種々のソフトウェア(インストール)LiteCLI のインストール,データベース作成,テーブル定義,レコード挿入,SQL問い合わせ,主なコマンド(Windows 上)

LiteCLI のインストール,データベース作成,テーブル定義,レコード挿入,SQL問い合わせ,主なコマンド(Windows 上)

LiteCLI は,コードアシスト機能付きの,SQLite 3 コマンドラインクライアント

目次

  1. LiteCLI のインストール(Windows 上)
  2. LiteCLI の起動と終了,ヘルプの表示,エンコーディングの確認
  3. データベースの新規作成
  4. テーブル定義,レコード挿入,SQL問い合わせ
  5. LiteCLI の主なコマンド

LiteCLI のインストール(Windows 上)

  1. Windows のコマンドプロンプトを管理者として実行する.
  2. 次のコマンドを実行する.

    python -m pip install -q git+https://github.com/dbcli/litecli.git
    

    [image]
  3. 確認のため litecli を起動してみる.

    litecli
    

    [image]

    「exit」で終了

    exit
    

    [image]

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

SQLite 3 の説明は https://www.sqlite.org/sqlite.html

データベースの新規作成

ここでの設定

  1. litecli を実行する.
    litecli
    

    [image]
  2. データベースのオープン
    .open C:/sqlite3/hoge.db
    

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

    [image]

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

  1. litecli を実行する.
    litecli
    

    [image]
  2. データベースオープン
    .open 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 with time zone not null,
        updated_at    timestamp with time zone,
        check ( ( unit_price * qty ) < 200000 ) );
    

    [image]
  4. 「.tables」を実行して,テーブルが定義できたことを確認.
    .tables 
    

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

    begin transaction;
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty, created_at) values( 1, 2022, 1, 26,  'kaneko', 'orange A', 1.2, 10, datetime('now', 'localtime') );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty, created_at) values( 2, 2022, 1, 26,  'miyamoto', 'Apple M',  2.5, 2, datetime('now', 'localtime') );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty, created_at) values( 3, 2022, 1, 27,  'kaneko',   'orange B', 1.2, 8, datetime('now', 'localtime') );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, created_at) values( 4, 2022, 1, 28,  'miyamoto',   'Apple L', 3, datetime('now', 'localtime') );
    commit;
    
  6. SQL 問い合わせ

    select * from order_records;
    
  7. 「exit」を実行して,litecli を終了.
    exit
    

    [image]

LiteCLI の主なコマンド

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