powershell:sqlite_import_csv
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| powershell:sqlite_import_csv [2025/08/26 23:22] – mikoto | powershell:sqlite_import_csv [2025/10/15 07:29] (現在) – mikoto | ||
|---|---|---|---|
| 行 2: | 行 2: | ||
| [[: | [[: | ||
| + | ===== 例1 ===== | ||
| <code powershell> | <code powershell> | ||
| - | # SQLite | + | # CSVやTSVのデータをSQLiteでデータベースにインポートする |
| + | |||
| + | # CSVを読み込み | ||
| + | #$data = Import-Csv -Path .\list.tsv -Delimiter " | ||
| + | #$data = Import-Csv -Path .\list.csv -Delimiter ";" | ||
| + | $data = Import-Csv -Path .\list.csv | ||
| + | $data | Format-Table | ||
| + | |||
| + | |||
| + | # sqlite3.dll を読み込む | ||
| Add-Type -Path " | Add-Type -Path " | ||
| - | # SQLite データベースのパス | + | # DBのファイルパス(無ければ新規作成) |
| - | $dbPath | + | $dbFile |
| - | $connectionString = "Data Source=$dbPath; | + | $connectionString = "Data Source=$dbFile; |
| - | # 接続を開く | + | if (-not (Test-Path $dbFile)) { |
| - | $connection = New-Object | + | [System.Data.SQLite.SQLiteConnection]:: |
| - | $connection.Open() | + | } |
| - | # テーブル作成(無ければ作る) | + | # SQLite 接続オブジェクトを作成 |
| - | $command | + | $conn = New-Object System.Data.SQLite.SQLiteConnection($connectionString) |
| - | $command.CommandText = @" | + | $conn.Open() |
| - | CREATE TABLE IF NOT EXISTS | + | |
| - | | + | # コマンドオブジェクト |
| - | | + | $cmd = $conn.CreateCommand() |
| - | | + | |
| + | # テーブルがない場合は作成 | ||
| + | $cmd.CommandText = @" | ||
| + | CREATE TABLE IF NOT EXISTS | ||
| + | | ||
| + | | ||
| + | | ||
| ); | ); | ||
| "@ | "@ | ||
| - | $command.ExecuteNonQuery() | Out-Null | ||
| - | # CSV を読み込み | + | $cmd.ExecuteNonQuery() |
| - | $csvPath = ".\data.csv" | + | |
| - | $rows = Import-Csv -Path $csvPath | + | |
| - | # トランザクション開始(高速化) | ||
| - | $transaction = $connection.BeginTransaction() | ||
| - | foreach ($row in $rows) { | + | foreach ($row in $data) { |
| - | $command | + | $cmd = $conn.CreateCommand() |
| - | $command.CommandText = " | + | $cmd.CommandText = " |
| + | $cmd.Parameters.AddWithValue(" | ||
| + | $cmd.Parameters.AddWithValue(" | ||
| + | $cmd.Parameters.AddWithValue(" | ||
| + | $cmd.ExecuteNonQuery() | ||
| + | } | ||
| - | # パラメータに値をバインド | + | $conn.Close() |
| - | | + | </ |
| - | | + | |
| - | $command.Parameters.Add((New-Object System.Data.SQLite.SQLiteParameter(" | + | |
| - | | + | ===== 例2 ===== |
| + | トランザクションを使うことによって、書き込み処理をまとめて実行することができるため処理を高速化できる。 | ||
| + | **$conn.BeginTransaction()**でトランザクションを開始し、**Comit()**で処理を確定する。 | ||
| + | <code powershell> | ||
| + | Add-Type -AssemblyName System.Windows.Forms | ||
| + | Add-Type -AssemblyName System.Drawing | ||
| + | |||
| + | # sqlite3.dll を読み込む | ||
| + | Add-Type -Path " | ||
| + | |||
| + | # DBのファイルパス(無ければ新規作成) | ||
| + | $dbFile = " | ||
| + | $connStr = "Data Source=$dbFile; | ||
| + | |||
| + | # SQLite 接続オブジェクトを作成 | ||
| + | $conn = New-Object System.Data.SQLite.SQLiteConnection($connStr) | ||
| + | $conn.Open() | ||
| + | |||
| + | # CSVデータの読み込み | ||
| + | $dialog = New-Object System.Windows.Forms.OpenFileDialog | ||
| + | $dialog.Filter = " | ||
| + | $dialog_result = $dialog.ShowDialog() | ||
| + | |||
| + | if ($dialog_result -eq [System.Windows.Forms.DialogResult]:: | ||
| + | $csvPath = $dialog.FileName | ||
| + | $csvData = Import-Csv -Path $csvPath | ||
| + | |||
| + | # 処理の高速化のためのトランザクション開始 | ||
| + | $tran = $conn.BeginTransaction() | ||
| + | $cmd = $conn.CreateCommand() | ||
| + | $cmd.Transaction = $tran | ||
| + | $cmd.CommandText = " | ||
| + | |||
| + | # パラメータを一度だけ作成する | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | |||
| + | foreach ($row in $csvData) { | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.ExecuteNonQuery() | Out-Null | ||
| + | } | ||
| + | |||
| + | # トランザクションをコミットする | ||
| + | $tran.Commit() | ||
| + | |||
| + | } else { | ||
| + | [System.Windows.Forms.MessageBox]:: | ||
| } | } | ||
| - | # コミット | ||
| - | $transaction.Commit() | ||
| - | # 接続を閉じる | + | $conn.Close() |
| - | $connection.Close() | + | </ |
| + | |||
| + | ===== TSVのインポート ===== | ||
| + | |||
| + | <code powershell> | ||
| + | Add-Type -AssemblyName System.Windows.Forms | ||
| + | Add-Type -AssemblyName System.Drawing | ||
| + | |||
| + | # sqlite3.dll を読み込む | ||
| + | Add-Type -Path " | ||
| + | |||
| + | # DBのファイルパス(無ければ新規作成) | ||
| + | $dbFile = " | ||
| + | $connStr = "Data Source=$dbFile; | ||
| + | |||
| + | $sql = @" | ||
| + | INSERT INTO share | ||
| + | (code, campany, quantity, obtained_price, | ||
| + | VALUES | ||
| + | (@code, @campany, @quantity, @obtained_price, | ||
| + | "@ | ||
| + | |||
| + | # SQLite | ||
| + | $conn = New-Object System.Data.SQLite.SQLiteConnection($connStr) | ||
| + | $conn.Open() | ||
| + | |||
| + | # CSVデータの読み込み | ||
| + | $dialog = New-Object System.Windows.Forms.OpenFileDialog | ||
| + | # | ||
| + | $dialog.Filter = " | ||
| + | $dialog_result = $dialog.ShowDialog() | ||
| + | |||
| + | if ($dialog_result -eq [System.Windows.Forms.DialogResult]:: | ||
| + | $csvPath = $dialog.FileName | ||
| + | $csvData = Import-Csv -Path $csvPath -Delimiter " | ||
| + | $csvData | ||
| + | # 処理の高速化のためのトランザクション開始 | ||
| + | $tran = $conn.BeginTransaction() | ||
| + | $cmd = $conn.CreateCommand() | ||
| + | $cmd.Transaction = $tran | ||
| + | $cmd.CommandText = $sql | ||
| + | # パラメータを一度だけ作成する | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | $null = $cmd.Parameters.Add(" | ||
| + | |||
| + | foreach ($row in $csvData) { | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.Parameters[" | ||
| + | $cmd.ExecuteNonQuery() | Out-Null | ||
| + | } | ||
| + | |||
| + | # トランザクションをコミットする | ||
| + | $tran.Commit() | ||
| + | |||
| + | } else { | ||
| + | [System.Windows.Forms.MessageBox]:: | ||
| + | } | ||
| - | Write-Host " | + | $conn.Close() |
| </ | </ | ||
powershell/sqlite_import_csv.1756218142.txt.gz · 最終更新: 2025/08/26 23:22 by mikoto