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

LiteCLI は,コード補完機能と構文強調表示の機能を持つ SQLite 3 のコマンドラインクライアントである。

目次

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

前準備

Ubuntu のシステム更新

Ubuntu で OS のシステム更新を行うときは, 端末で,次のコマンドを実行する。これは、パッケージ情報を最新の状態に保ち、インストール済みのパッケージをセキュリティアップデートやバグ修正を含めて更新するためである。

Ubuntu のインストールはこちらの別ページで説明する。

# パッケージリストの情報を更新
sudo apt update
# インストール済みのパッケージを包括的に更新 (依存関係も考慮)
sudo apt full-upgrade
# カーネル更新等で実際に再起動が必要な場合のみ実行を推奨
# sudo shutdown -r now

LiteCLI のインストール

LiteCLI は Ubuntu の公式リポジトリ(universe)で litecli パッケージとして提供されている。Ubuntu 22.04 LTS 以降で利用できる。

Ubuntu 23.04 以降のシステム Python は externally-managed-environment として保護されており,sudo pip3 install によるシステム全体へのインストールはエラーとなる。Ubuntu のパッケージ(apt)を用いるか,アプリケーションを個別の仮想環境に導入する pipx を用いる。
  1. インストール

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

    # パッケージリストの情報を更新
    sudo apt update
    sudo apt -y install litecli
    

    最新版を使いたい場合は,端末で,次のコマンドを実行し,pipx を用いて公式リポジトリ(PyPI)の版をインストールする.pipx は,コマンドラインアプリケーションを専用の仮想環境に隔離してインストールする.

    # パッケージリストの情報を更新
    sudo apt update
    sudo apt -y install pipx
    pipx install litecli
    pipx ensurepath
    

    pipx ensurepath は,インストール先である ~/.local/bin を PATH に追加する。追加後は端末を開き直す.

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

    「exit」で終了

    exit
    

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

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

データベースの新規作成

ここでの設定

  1. litecli を実行する.
    cd ~
    litecli
    
  2. データベースのオープン

    指定したファイルが存在しないときは,新しいデータベースファイルとして作成される.

    .open hoge.db
    
  3. 「exit」を実行して,litecli を終了.
    exit
    

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

  1. litecli を実行する.
    cd ~
    litecli
    
  2. データベースオープン
    .open hoge.db
    
  3. テーブル定義

    check 制約により,年,月,日,単価,数量,および合計金額の範囲を検査する.

    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 ) );
    
  4. 「.tables」を実行して,テーブルが定義できたことを確認.
    .tables
    
  5. SQL を用いたレコード挿入

    複数の insert 文を begin transactioncommit で囲み,1つのトランザクションとして実行する.

    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
    

LiteCLI の主なコマンド

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