ユーザ用ツール

サイト用ツール


powershell:form_sqlite_db_manageer

簡易DBマネージャー

一つ上へ

作成中。

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
 
# sqlite3.dll を読み込む
Add-Type -Path "path\to\dll\System.Data.SQLite.dll"
 
# DBのファイルパス(無ければ新規作成)
$dbFile = "path\to\database\database.db"
$connStr = "Data Source=$dbFile;Version=3;"
 
<# --- 関数定義 Begin--- #>
function Show-Tablelist {
    param (
        [System.Data.SQLite.SQLiteConnection]$connection,
        [System.Windows.Forms.ListBox]$listbox
    )
 
    $cmd = $connection.CreateCommand()
    $cmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;"
    $reader = $cmd.ExecuteReader() # Readの時はOut-Nullの必要なし
 
    $listbox.Items.Clear()
 
    while ($reader.Read()) {
        $tablename = $reader["name"]
        $listbox.Items.Add($tablename)
    }
    $reader.Close()
}
 
<# --- 関数定義 End #>
if (-not (Test-Path $dbFile)) {
    [System.Data.SQLite.SQLiteConnection]::CreateFile($dbFile)
}
 
# SQLite 接続オブジェクトを作成
$conn = New-Object System.Data.SQLite.SQLiteConnection($connStr)
$conn.Open()
 
# コマンドオブジェクト
$cmd = $conn.CreateCommand()
 
 
# フォーム作成
$form = New-Object System.Windows.Forms.Form
$form.Text = "データベース管理"
$form.MinimumSize = New-Object System.Drawing.Size(900,600)
$form.Size = New-Object System.Drawing.Size(1200,800)
 
# テーブルリスト
$tableListLabel = New-Object System.Windows.Forms.Label
$tableListLabel.Text = "テーブルリスト"
$tableListLabel.Location = New-Object System.Drawing.Point(20,20)
 
 
$tableList = New-Object System.Windows.Forms.ListBox
$tableList.Location = New-Object System.Drawing.Point(20,50)
$tableList.Size = New-Object System.Drawing.Size(200,300)
 
Show-Tablelist -connection $conn -listbox $tableList
 
 
# --- データインポートボタン ---
$btnImport = New-Object System.Windows.Forms.Button
$btnImport.Text = "インポート"
$btnImport.Location = New-Object System.Drawing.Point(100,20)
 
# データインポート処理
$btnImport.Add_Click({
    $dialog = New-Object System.Windows.Forms.OpenFileDialog
    #$dialog.InitialDirectory = [Environment]::GetFolderPath("Desktop") # 初期フォルダ
    $dialog.Filter = "CSVファイル(*.csv)|*.csv|All files (*.*)|*.*"
    $dialog_result = $dialog.ShowDialog()
 
    if ($dialog_result -eq [System.Windows.Forms.DialogResult]::OK) {
        $filePath = $dialog.FileName
        Write-Host "選択されたファイル: $filePath"
    } else {
        [System.Windows.Forms.MessageBox]::Show("処理を中止しました。")
    }
})
 
# テーブル削除
$btnTbDrop = New-Object System.Windows.Forms.Button
$btnTbDrop.Location = New-Object System.Drawing.Point(200,20)
$btnTbDrop.Width = 100
$btnTbDrop.Text = "テーブル削除"
 
$btnTbDrop.Add_Click({
    if ( $tableList.SelectedItem -ne $null ) {
        $selected_table_name = [string]$tableList.SelectedItem
        $cmd.CommandText = "DROP TABLE IF EXISTS [$selected_table_name];"
        $cmd.ExecuteNonQuery()
        [System.Windows.Forms.MessageBox]::Show("テーブルが削除されました。", "確認")
        try {
            Show-Tablelist -connection $conn -listbox $tableList
        } catch {
            [System.Windows.Forms.MessageBox]::Show("例外が発生しました。", "警告")
        }
    } else {
        [System.Windows.Forms.MessageBox]::Show("テーブルが選択されていません。", "確認")
    }
})
 
# テーブルデータの削除
$btnDeleteTableData = New-Object System.Windows.Forms.Button
$btnDeleteTableData.Location = New-Object System.Drawing.Point(500,20)
$btnDeleteTableData.Width = 100
$btnDeleteTableData.Text = "テーブルデータ削除"
 
$btnDeleteTableData.Add_Click({
    if ($tableList.SelectedItem -ne $null) {
        $selected_table_name = $tableList.SelectedItem
        $user_result = [System.Windows.Forms.MessageBox]::Show(
            "$selected_table_name のデータを削除しますか。",
            "確認",
            [System.Windows.Forms.MessageBoxButtons]::YesNo,
            [System.Windows.Forms.MessageBoxIcon]::Question
        )
        if ($user_result -eq [System.Windows.Forms.DialogResult]::Yes){
            $cmd.CommandText = "DELETE FROM [$selected_table_name];"
            $null = $cmd.ExecuteNonQuery()
            [System.Windows.Forms.MessageBox]::Show("データを削除しました。")
        } else {
            [System.Windows.Forms.MessageBox]::Show("キャンセルしました。")
        }
    } else {
        [System.Windows.Forms.MessageBox]::Show("テーブルが選択されていません。")
    }
})
 
# データの表示
$dataGridArea = New-Object System.Windows.Forms.DataGridView
$dataGridArea.Location = New-Object System.Drawing.Point(400,200)
$dataGridArea.Size = New-Object System.Drawing.Size(400,400)
$dataGridArea.AutoSizeColumnsMode = "Fill"
$dataGridArea.ReadOnly = $true
$dataGridArea.Anchor = [System.Windows.Forms.AnchorStyles]::Top `
    -bor [System.Windows.Forms.AnchorStyles]::Bottom `
    -bor [System.Windows.Forms.AnchorStyles]::Left `
    -bor [System.Windows.Forms.AnchorStyles]::Right
 
# セルを選択したときにその値を表示する
$dataGridArea.Add_CellClick({
    param($s, $e)
    $rowIndex = $e.RowIndex
    $colIndex = $e.ColumnIndex
 
    if ($rowIndex -ge 0 -and $colIndex -ge 0) {
        $cellValue = $s.Rows[$rowIndex].Cells[$colIndex].Value
        [System.Windows.Forms.MessageBox]::Show("$cellValue", "セル情報")
    }
})
 
 
# コントロールを追加
$form.Controls.Add($tableListLabel)
$form.Controls.Add($btnImport)
$form.Controls.Add($tableList)
$form.Controls.Add($btnTbDrop)
$form.Controls.Add($btnDeleteTableData)
$form.Controls.Add($dataGridArea)
 
$form.Add_FormClosed({ $conn.Close() })
 
# フォームを表示
[void]$form.ShowDialog()
powershell/form_sqlite_db_manageer.txt · 最終更新: 2025/10/14 00:53 by mikoto