Paradigm Shift Design

ISHITOYA Kentaro's blog.

SQLiteをC#で使う

導入手順やらは,C#からSQLiteを使ってみた - mitc - 日記にあるように,

  1. System.Data.SQLite から,1.0.x系をダウンロードする
  2. インストールするか解凍してできたディレクトリのSystem.Data.SQLite.XMLとSystem.Data.SQLite.dllを,プロジェクトの適当なディレクトリに放り込む
  3. 参照設定を右クリックして参照の追加をクリック
  4. 参照の追加ダイアログの参照タブから,System.Data.SQLite.dllを選択してOK
  5. 完了


です.とても簡単.
で,使う側なんだけれども,

try
{
    if (Directory.Exists("logs") == false)
    {
        Directory.CreateDirectory("logs");
    }

    System.Reflection.Assembly assembly =
        System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.StreamReader sr =
        new System.IO.StreamReader(
            assembly.GetManifestResourceStream("cc.guarana.kent.Resources.setup.txt"), System.Text.Encoding.GetEncoding("shift-jis"));
    //内容を読み込む
    string create = sr.ReadToEnd();
    sr.Close();

    this.connection = new SQLiteConnection(@"Data Source=logs\log_" + UnixEpochTime.now() + ".db");
    this.connection.Open();
    SQLiteTransaction transaction = this.connection.BeginTransaction();
    SQLiteCommand command = this.connection.CreateCommand();
    command.CommandText = create;
    command.ExecuteNonQuery();
    transaction.Commit();
    transaction.Dispose();
}
catch (SQLiteException exception)
{
    Console.WriteLine(exception.Message);
}

のように,必ず,Transactionで囲うこと.
囲わないと,異様に重い.

ページが見つかりません | ランディネットワーク

にもあるけれど,INSERTというか更新処理はすべて重い.
Transactionで囲ったら,時間が100分の1になる感じです.


あと,cygwinのsqlite3.exeはクソったれちゃんなので,SQLite ControlCenter 日本語版あたりを使うのがいいです.
本家はSQLite Control Center - SQLiteCC



2009/03/13:追記
sqliteファイルの中身を確認するには、FireFoxプラグインのSQLite Manager :: Add-ons for Firefoxを使うのが一番いいようです。
Firefox上で動作するSQLiteフロントエンド「SQLite Manager」 - MOONGIFT|オープンソース・ソフトウェア紹介を軸としたITエンジニア、Webデザイナー向けブログ