powershell:sqlite_import_csv
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン | |||
| powershell:sqlite_import_csv [2025/10/14 00:49] – mikoto | powershell:sqlite_import_csv [2025/10/15 07:29] (現在) – mikoto | ||
|---|---|---|---|
| 行 97: | 行 97: | ||
| $cmd.Parameters[" | $cmd.Parameters[" | ||
| $cmd.Parameters[" | $cmd.Parameters[" | ||
| + | $cmd.ExecuteNonQuery() | Out-Null | ||
| + | } | ||
| + | |||
| + | # トランザクションをコミットする | ||
| + | $tran.Commit() | ||
| + | |||
| + | } else { | ||
| + | [System.Windows.Forms.MessageBox]:: | ||
| + | } | ||
| + | |||
| + | |||
| + | $conn.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 | $cmd.ExecuteNonQuery() | Out-Null | ||
| } | } | ||
powershell/sqlite_import_csv.1760370550.txt.gz · 最終更新: 2025/10/14 00:49 by mikoto