金子邦彦研究室インストールUbuntu, WSL2LiteCLI のインストール,データベース作成,テーブル定義,レコード挿入,SQL問い合わせ,主なコマンド(Ubuntu 上)

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

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

目次

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

前準備

Ubuntu のシステム更新

UbuntuUbuntu で OS のシステム更新を行うときは, 端末で,次のコマンドを実行する.

UbuntuUbuntu のインストールは別ページ »で説明

sudo apt -y update
sudo apt -yV upgrade
sudo /sbin/shutdown -r now

Python3 開発用ファイル,pip, setuptools, venv のインストール(Ubuntu 上)

Python のインストールは行わない(Ubuntu のシステム Python を用いる.)

Python, pip のコマンドでの起動のまとめ.

Ubuntu のシステム Python を用いるとき, python, pip は,次のコマンドで起動できる.

Ubuntu での Python 開発環境(JupyterLab, spyder, nteract)のインストール: 別ページ »で説明している.

Python3 開発用ファイル,pip, setuptools, venv のインストール

端末で,次のコマンドを実行する.

sudo apt -y update
sudo apt -y install python-is-python3 python3-dev python-dev-is-python3 python3-pip python3-setuptools python3-venv build-essential

LiteCLI のインストール

  1. インストール

    端末で,次のコマンドを実行する.

    sudo pip3 install -q git+https://github.com/dbcli/litecli.git
    

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

    litecli
    

    [image]

    「exit」で終了

    exit
    

    [image]

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

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

データベースの新規作成

ここでの設定

  1. litecli を実行する.
    litecli
    

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

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

    [image]

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

  1. litecli を実行する.
    litecli
    

    [image]
  2. データベースオープン
    .open 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;
    

    [image]
  6. SQL 問い合わせ

    select * from order_records;
    

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

    [image]

LiteCLI の主なコマンド

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