powershell:form_sqlite_db_client
差分
このページの2つのバージョン間の差分を表示します。
次のリビジョン | 前のリビジョン | ||
powershell:form_sqlite_db_client [2025/09/24 01:18] – 作成 mikoto | powershell:form_sqlite_db_client [2025/10/02 07:38] (現在) – mikoto | ||
---|---|---|---|
行 3: | 行 3: | ||
<code powershell> | <code powershell> | ||
+ | # Form用のモジュールを読み込み | ||
Add-Type -AssemblyName System.Windows.Forms | Add-Type -AssemblyName System.Windows.Forms | ||
Add-Type -AssemblyName System.Drawing | Add-Type -AssemblyName System.Drawing | ||
# sqlite3.dll を読み込む | # sqlite3.dll を読み込む | ||
- | Add-Type -Path ".\System.Data.SQLite.dll" | + | Add-Type -Path "path\to\dll\System.Data.SQLite.dll" |
- | # フォーム作成 | + | # --- クエリ実行関数 --- |
- | $form = New-Object | + | function showTableList { |
- | $form.Text = "Easy DB Client" | + | param( |
- | $form.Size = New-Object System.Drawing.Size(1000, | + | [System.Data.SQLite.SQLiteConnection]$Connection |
+ | ) | ||
- | # DBのファイルパス(無ければ新規作成) | + | $cmd = $Connection.CreateCommand() |
- | $dbFile = ".\test.db" | + | $cmd.CommandText |
- | $connectionString | + | |
- | if (-not (Test-Path | + | try { |
- | [System.Data.SQLite.SQLiteConnection]:: | + | $results = New-Object System.Collections.Generic.List[object] |
+ | |||
+ | $reader = $cmd.ExecuteReader() | ||
+ | while ($reader.Read()) { | ||
+ | $row = [PSCustomObject]@{ | ||
+ | name = $reader[" | ||
+ | } | ||
+ | $results.Add($row) | Out-Null | ||
+ | } | ||
+ | $reader.Close() | ||
+ | return $results | ||
+ | } | ||
+ | catch { | ||
+ | return $null | ||
+ | } | ||
} | } | ||
+ | # --- テーブルデータを表示 --- | ||
+ | function showData { | ||
+ | param ( | ||
+ | [System.Data.SQLite.SQLiteConnection]$Connection | ||
+ | ) | ||
+ | |||
+ | $cmd = $Connection.CreateCommand() | ||
+ | $cmd.CommandText = " | ||
+ | |||
+ | try { | ||
+ | $results = New-Object System.Collections.Generic.List[object] | ||
+ | return $null | ||
+ | } | ||
+ | catch { | ||
+ | return $null | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # 初期化処理 | ||
+ | # DBのファイルパス(無ければ新規作成) | ||
+ | $dbFile = " | ||
+ | $connStr = "Data Source=$dbFile; | ||
# SQLite 接続オブジェクトを作成 | # SQLite 接続オブジェクトを作成 | ||
- | $conn = New-Object System.Data.SQLite.SQLiteConnection($connectionString) | + | $conn = New-Object System.Data.SQLite.SQLiteConnection($connStr) |
$conn.Open() | $conn.Open() | ||
- | # コマンドオブジェクト | ||
- | $cmd = $conn.CreateCommand() | ||
- | # --- テーブルリスト表示 --- | + | # フォーム作成 |
- | $cmd.CommandText | + | $form = New-Object System.Windows.Forms.Form |
+ | $form.Text = "簡易DBクライアント" | ||
+ | $form.Size = New-Object System.Drawing.Size(1200, | ||
- | $labelTable | + | # テーブル一覧表示領域ラベル |
- | $labelTable.Text = " | + | $tbListLabel |
- | $labelTable.Location = New-Object System.Drawing.Point(20, | + | $tbListLabel.Text = " |
- | $labelTable.Size = New-Object System.Drawing.Size(100, | + | $tbListLabel.Location = New-Object System.Drawing.Point(20, |
+ | $tbListLabel.Size = New-Object System.Drawing.Size(100, | ||
- | $listBoxTable | + | # テーブル一覧表示領域 |
- | $listBoxTable.Size = New-Object System.Drawing.Size(300, | + | $tbListArea |
- | $listBoxTable.Location = New-Object System.Drawing.Point(20, | + | #$tbListArea.Size = New-Object System.Drawing.Size(300, |
+ | $tbListArea.Location = New-Object System.Drawing.Point(20, | ||
+ | $tbListArea.Width = 300 | ||
+ | $tbListArea.Height = $form.ClientSize.Height - $tbListArea.Top - 20 # initial height | ||
+ | $tbListArea.Anchor = [System.Windows.Forms.AnchorStyles]:: | ||
+ | -bor [System.Windows.Forms.AnchorStyles]:: | ||
+ | -bor [System.Windows.Forms.AnchorStyles]:: | ||
- | # 実行 | + | $tbList |
- | $reader | + | foreach |
- | while ($reader.Read()) { | + | $tbListArea.Items.Add($t.name) |
- | $tableName = $reader[" | + | |
- | $listBoxTable.Items.Add($tableName) | + | |
} | } | ||
- | $reader.Close() | ||
- | |||
- | # レコード表示ボタン | ||
- | $btnShow = New-Object System.Windows.Forms.Button | ||
- | $btnShow.Text = " | ||
- | $btnShow.Location = New-Object System.Drawing.Point(20, | ||
- | |||
- | # 保存ボタン | ||
- | $btnSave = New-Object System.Windows.Forms.Button | ||
- | $btnSave.Text = " | ||
- | $btnSave.Location = New-Object System.Drawing.Point(100, | ||
# DataGridView | # DataGridView | ||
$dataGrid = New-Object System.Windows.Forms.DataGridView | $dataGrid = New-Object System.Windows.Forms.DataGridView | ||
- | $dataGrid.Location = New-Object System.Drawing.Point(350, | + | $dataGrid.Location = New-Object System.Drawing.Point(350, |
- | $dataGrid.Size = New-Object System.Drawing.Size(600, | + | $dataGrid.Height |
+ | $dataGrid.Width = $form.ClientSize.Width - $tbListArea.Right - 50 | ||
$dataGrid.AutoSizeColumnsMode = " | $dataGrid.AutoSizeColumnsMode = " | ||
- | $dataGrid.AllowUserToAddRows = $true | + | #$dataGrid.AllowUserToAddRows = $true |
- | $dataGrid.AllowUserToDeleteRows = $true | + | #$dataGrid.AllowUserToDeleteRows = $true |
- | $dataGrid.ReadOnly = $false | + | $dataGrid.ReadOnly = $true |
+ | $dataGrid.Anchor = [System.Windows.Forms.AnchorStyles]:: | ||
+ | -bor [System.Windows.Forms.AnchorStyles]:: | ||
+ | -bor [System.Windows.Forms.AnchorStyles]:: | ||
+ | -bor [System.Windows.Forms.AnchorStyles]:: | ||
- | # 変数をフォームスコープで共有 | + | # レコード表示ボタン |
- | $dataTable | + | $btnShow |
- | $adapter | + | $btnShow.Text |
- | $table = $null | + | $btnShow.Location |
# レコード表示ボタンイベント | # レコード表示ボタンイベント | ||
$btnShow.Add_Click({ | $btnShow.Add_Click({ | ||
- | if ($listBoxTable.SelectedItem -ne $null) { | + | |
- | $table = [string]$listBoxTable.SelectedItem | + | Write-Host $tbListArea.SelectedItem |
+ | | ||
+ | $table = [string]$tbListArea.SelectedItem | ||
+ | $cmd = $conn.CreateCommand() | ||
$cmd.CommandText = " | $cmd.CommandText = " | ||
行 91: | 行 130: | ||
}) | }) | ||
- | # 保存ボタンクリックイベント | + | # コントロールを追加 |
- | $btnSave.Add_Click({ | + | $form.Controls.Add($tbListLabel) |
- | if ($script: | + | $form.Controls.Add($tbListArea) |
- | try { | + | |
- | # DataGridView の編集中セルを確定 | + | |
- | if ($dataGrid.IsCurrentCellInEditMode) {$dataGrid.EndEdit() } | + | |
- | $dataGrid.CurrentCell = $null # フォーカスを外して編集を確定 | + | |
- | + | ||
- | # DataTableの変更を確認 | + | |
- | $changes = $script: | + | |
- | if ($changes -ne $null) { | + | |
- | $script: | + | |
- | Write-Host " | + | |
- | Write-Host " | + | |
- | Write-Host " | + | |
- | [System.Windows.Forms.MessageBox]:: | + | |
- | } else { | + | |
- | [System.Windows.Forms.MessageBox]:: | + | |
- | } | + | |
- | } | + | |
- | catch { | + | |
- | [System.Windows.Forms.MessageBox]:: | + | |
- | } | + | |
- | } else { | + | |
- | [System.Windows.Forms.MessageBox]:: | + | |
- | } | + | |
- | }) | + | |
- | + | ||
- | # コントロール | + | |
- | $form.Controls.Add($labelTable) | + | |
- | $form.Controls.Add($listBoxTable) | + | |
- | $form.Controls.Add($btnShow) | + | |
$form.Controls.Add($dataGrid) | $form.Controls.Add($dataGrid) | ||
- | $form.Controls.Add($btnSave) | + | $form.Controls.Add($btnShow) |
# --- フォームを閉じたら DB も閉じる --- | # --- フォームを閉じたら DB も閉じる --- | ||
行 131: | 行 141: | ||
# フォームを表示 | # フォームを表示 | ||
[void]$form.ShowDialog() | [void]$form.ShowDialog() | ||
+ | </ | ||
+ | ===== 検索機能の実装 ===== | ||
+ | |||
+ | <code powershell> | ||
+ | # --- 検索用関数 --- | ||
+ | function searchRecords { | ||
+ | param ( | ||
+ | [System.Data.SQLite.SQLiteConnection]$connection, | ||
+ | [string]$table_name, | ||
+ | [string]$keyword | ||
+ | ) | ||
+ | |||
+ | $dataTable = New-Object System.Data.DataTable | ||
+ | $cmd = $connection.CreateCommand() | ||
+ | $cmd.CommandText = " | ||
+ | # $null = $cmd.Parameters.AddWithValue(" | ||
+ | |||
+ | $adapter = New-Object System.Data.SQLite.SQLiteDataAdapter($cmd) | ||
+ | $adapter.Fill($dataTable) | Out-Null | ||
+ | return $dataTable | ||
+ | } | ||
+ | |||
+ | # 検索ボタン | ||
+ | $btnSearch = New-Object System.Windows.Forms.Button | ||
+ | $btnSearch.Text = " | ||
+ | $btnSearch.Location = New-Object System.Drawing.Point(430, | ||
+ | |||
+ | # 検索ボタンクリックイベント | ||
+ | $btnSearch.Add_Click({ | ||
+ | if ([string]$tbListArea.SelectedItem -ne $null) { | ||
+ | $table_name = ([string]$tbListArea.SelectedItem).Trim() | ||
+ | $keyword = [string]$textSearch.Text | ||
+ | # $dataTable = searchRecords -connection $conn -table_name $table_name -keyword $keyword | ||
+ | $dataGrid.DataSource = $dataTable | ||
+ | $dataTable | Format-Table -AutoSize | ||
+ | } else { | ||
+ | Write-Host " | ||
+ | } | ||
+ | }) | ||
</ | </ |
powershell/form_sqlite_db_client.1758644336.txt.gz · 最終更新: 2025/09/24 01:18 by mikoto