一つ上へ
# --- SQLite 初期化 ---
Add-Type -Path "path\to\dll\System.Data.SQLite.dll"
$dbFile = "path\to\database\file.db"
$connStr = "Data Source=$dbFile;Version=3;"
$conn = New-Object System.Data.SQLite.SQLiteConnection($connStr)
$conn.Open()
# --- クエリ実行関数 ---
function Invoke-SqliteQuery {
param(
[string]$Sql,
[System.Data.SQLite.SQLiteConnection]$Connection
)
$cmd = $Connection.CreateCommand()
$cmd.CommandText = $Sql
try {
$reader = $cmd.ExecuteReader()
$results = @()
while ($reader.Read()) {
$row = @{}
for ($i = 0; $i -lt $reader.FieldCount; $i++) {
$row[$reader.GetName($i)] = $reader.GetValue($i)
}
$results += [PSCustomObject]$row
}
$reader.Close()
return $results
}
catch {
# SELECT以外(INSERT/UPDATE/DELETE)は ExecuteNonQuery
$null = $cmd.ExecuteNonQuery()
return $null
}
}
# --- 対話モード ---
Write-Host "SQLite CUI クライアントを起動しました。"
Write-Host "SQL> にクエリを入力してください。終了するには 'exit' または 'quit'"
while ($true) {
$sql = Read-Host "SQL>"
if ($sql -match "^(exit|quit)$") { break }
try {
$rows = Invoke-SqliteQuery -Sql $sql -Connection $conn
if ($rows) {
$rows | Format-Table -AutoSize
} else {
Write-Host "OK"
}
} catch {
Write-Warning $_.Exception.Message
}
}
$conn.Close()
Write-Host "SQLite クライアントを終了しました。"