2010年11月3日水曜日

SQLite を Windows で使ってみる

SQL を使ってデータ処理をしたいということがあると思います。
SQLite を使えば気軽に処理できます。
SQLite の特徴は、実行ファイル (sqlite3.exe) の引数に与えたファイルにデータベースが作成されるということです。


SQLite のインストール

Cygwin なら、SQLite が使えます。

Cygiwn が無い環境でも、実行ファイル sqlite3.exe を手に入れれば SQLite が使えます。
まず、SQLite の実行ファイルをこちら (http://www.sqlite.org/download.html) からダウンロードします。
sqlite-3_7_3.zip をダウンロード、展開して sqlite3.exe を取り出します。sqlite3.exe をパスの通った場所に置くか、カレントフォルダに置いておきます。


SQLite コマンドライン シェルを使ってみる


sqlite3.exe にデータベース (ファイル) を指定して、SQLite コマンドライン シェルに入ります。
ドット (.) で始まるコマンドは、SQLite コマンドライン シェルに対するコマンドです。
ドット (.) で始まらないコマンドは、SQL 文です。セミコロン (;) で終端します。
  • sqlite3.exe のオプションを調べる
    >sqlite3 -help
  • SQLite コマンドライン シェルのヘルプ
    sqlite> .help
  • テーブルの一覧を表示
    sqlite> .tables
  • インデックスの一覧を表示
    sqlite> .indices テーブル名
  • CREATE TABLE 文を表示する
    sqlite> .schema テーブル名
  • 設定を表示
    sqlite> .show
  • SELECT 文の出力結果にヘッダを付ける
    sqlite> .headers on (ヘッダを付ける)
    sqlite> .headers off (ヘッダを付けない)
  • SELECT 文の出力結果のフォーマットを変更する
    sqlite> .mode column (左詰めの列表示)
    sqlite> .mode line (一行毎に値を表示)
    sqlite> .mode csv (カンマ区切り)
    sqlite> .mode tabs (タブ区切り)
    sqlite> .mode list (.separator で指定される文字列で区切られる)
  • SQLite コマンドライン シェルを終了する
    sqlite> .exit (.quit)
  • SQL 文を実行する (セミコロン (;) で終端する SQL 文)
    sqlite> select * from test;
  • コマンドラインで直接 SQL 文を指定する。
    >sqlite3 example.sqlite3 "select * from test;"

SQL 文を実行してみる


WSUS の PUBLIC_VIEWS をインポートしてみる

こちらの方法で、タブ区切りで抽出したデータをインポートしてみます。
テーブルの作成 (CREATE TABLE) とデータのインポート (.import) を記述したファイル (wsus_create_table.sql) を作成します。

[wsus_create_table.sql]
.mode tabs

drop table if exists vCategory;
create table vCategory (
CategoryId text primary key,
CategoryType text,
ParentCategoryId text,
DefaultTitle text,
DefaultDescription text
);
.import vCategory.txt vCategory

drop table if exists vCategoryText;
create table vCategoryText (
CategoryId text,
LocaleId integer,
Title text,
Description text,
primary key (CategoryId, LocaleId)
);
.import vCategoryText.txt vCategoryText

drop table if exists vClassification;
create table vClassification (
ClassificationId text primary key,
DefaultTitle text,
DefaultDescription text
);
.import vClassification.txt vClassification

drop table if exists vClassificationText;
create table vClassificationText (
ClassificationId text,
LocaleId integer,
Title text,
Description text,
primary key (ClassificationId, LocaleId)
);
.import vClassificationText.txt vClassificationText

drop table if exists vComputerTargetGroup;
create table vComputerTargetGroup (
ComputerTargetGroupId text primary key,
Name text,
ParentTargetGroupId text
);
.import vComputerTargetGroup.txt vComputerTargetGroup

drop table if exists vUpdate;
create table vUpdate (
UpdateId text primary key,
RevisionNumber integer,
DefaultTitle text,
DefaultDescription text,
ClassificationId text,
ArrivalDate text,
CreationDate text,
IsDeclined integer,
IsWsusInfrastructureUpdate integer,
MsrcSeverity text,
PublicationState text,
UpdateType text,
UpdateSource text,
KnowledgebaseArticle text,
SecurityBulletin text,
InstallationCanRequestUserInput integer,
InstallationRequiresNetworkConnectivity integer,
InstallationImpact text,
InstallationRebootBehavior text
);
.import vUpdate.txt vUpdate

drop table if exists vUpdateApproval;
create table vUpdateApproval (
UpdateApprovalId text primary key,
UpdateId text not null,
ComputerTargetGroupId text not null,
Action text,
Deadline text,
CreationDate text,
AdministratorName text,
IsOptional integer,
IsStale integer,
unique (UpdateId, ComputerTargetGroupId)
);
.import vUpdateApproval.txt vUpdateApproval

drop table if exists vUpdateInCategory;
create table vUpdateInCategory (
UpdateId text,
CategoryId text,
CategoryType text,
primary key (UpdateId, CategoryId)
);
.import vUpdateInCategory.txt vUpdateInCategory

drop table if exists vUpdateText;
create table vUpdateText (
UpdateId text,
LocaleId integer,
Title text,
Description text,
primary key (UpdateId, LocaleId)
);
.import vUpdateText.txt vUpdateText

コマンド プロンプトから実行します。
>type wsus_create_table.sql | sqlite3 -echo wsus.sqlite3

PowerShell で SQLite を使う

ここ (http://sourceforge.net/projects/sqlite-dotnet2/) から、SQLite-1.0.66.0-binaries.zip をダウンロードして展開します。

bin\System.Data.SQLite.XML と bin\System.Data.SQLite.DLL
(64ビット版: bin\x64\System.Data.SQLite.DLL) を取り出して、好きな場所に設置します。
私は、カレントディレクトリに設置しました。

以下の例の様に使用します。
$path = (pwd).ToString()
[void][System.Reflection.Assembly]::LoadFile($path + "\System.Data.SQLite.DLL")
$sqlite = New-Object System.Data.SQLite.SQLiteConnection
$sqlite.ConnectionString = "Data Source = " + $path + "\wsus.sqlite3"
$sqlite.Open()
$cmd = "SELECT * FROM vClassification"
$sqlcmd = New-Object System.Data.SQLite.SQLiteCommand
$sqlcmd.Connection = $sqlite
$sqlcmd.CommandText = $cmd
$ret = $sqlcmd.ExecuteReader()
while ($ret.Read()) {
write ( "{0}`t{1}" -f $ret[0], $ret[1])
}
$sqlite.Close()


参考

0 件のコメント: