SQLiteman のソースコードからのインストール,データベース作成,テーブル定義(Windows 上)

SQLiteman は SQLite 3 のデータベースを操作するソフトウェアである. Windows 版,Linux 版,macOS 版があり,ソースコードも配布されている. 最新版は 1.2.2 で,ビルドには Qt 4 が必要である.

【目次】

  1. 前準備
  2. SQLiteman のインストール
  3. SQLite 3 の起動と終了,ヘルプの表示,エンコーディングの確認
  4. 空のデータベースの新規作成
  5. テーブル定義

前準備

SQLite 3 のインストール

マイクロソフト C++ ビルドツール (Build Tools) のインストール

Qt 4 のインストール

SQLiteman のインストール

  1. 新しいディレクトリ C:\sqlite3 を作る

    あとで,ここに データベースファイルを置く.

  2. SourceForge のウェブページを開く

    https://sourceforge.net/projects/sqliteman/

  3. 「Files」をクリック
  4. 「sqliteman」をクリック
  5. 最新版である「1.2.2」をクリック
  6. ソースコードである sqliteman-1.2.2.tar.gz を選ぶ
  7. ダウンロードが始まる.
  8. ダウンロードしたファイルを展開(解凍)する.
    Windows での展開(解凍)に便利な 7-Zip: 別ページ »で説明
  9. 展開(解凍)してできたファイルを,「C:\sqliteman-1.2.2」に移す.
  10. cmake の実行

    次のコマンドをx64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー → 「Visual C++ Build Tools」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ)。

    「x64 Native Tools コマンドプロンプト」がないときは,ビルドツール (Build Tools) をインストールする.その手順は,別ページ »で説明している.
    cd C:\sqliteman-1.2.2
    cmake -G "NMake Makefiles" -DWANT_RESOURCES=1 -DWANT_INTERNAL_QSCINTILLA=1 .
    
  11. cmake の結果の確認

    エラーメッセージが出ていないことを確認する.

  12. nmake の実行

    次のコマンドをx64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)で実行する   (手順:スタートメニュー → 「Visual C++ Build Tools」の下の「x64 Native Tools コマンドプロンプト (x64 Native Tools Command Prompt)」を選ぶ)。

    nmake
    
  13. nmake の結果の確認

    エラーメッセージが出ていないことを確認する.

  14. Windows の システム環境変数 Path に,C:\sqliteman-1.2.2\sqliteman を追加することにより,パスを通す.

    Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)。

    次のコマンドを実行する.

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

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

    where sqliteman
    
  16. 確認のため, Windows のコマンドプロンプトで,次のコマンドを実行する.
    sqliteman
    

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

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

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

ここでの設定

  1. SQLite を実行する.

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

    sqlite3
    
  2. 空のデータベースを保存する
    .open --new C:/sqlite3/hoge.db
    .exit
    

テーブル定義

  1. SQLite を実行し,データベースファイル C:\sqlite3\hoge.db を開く.

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

    sqlite3 C:/sqlite3/hoge.db
    
  2. SQL を用いたテーブル定義
    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    datetime not null default current_timestamp,
        updated_at    datetime,
        check ( ( unit_price * qty ) < 200000 ) );
    

    * テーブル名にはアルファベットのみを使う.

  3. 「.tables」を実行して,テーブルが定義できたことを確認する.
    .tables
    
  4. 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;
    
  5. 確認表示
    select * from order_records;
    
  6. SQLite 3 の終了
    .exit