powershell:sqlite
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| powershell:sqlite [2025/08/23 01:24] – mikoto | powershell:sqlite [2025/09/23 21:32] (現在) – mikoto | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== SQLiteでデータベースを作成する ====== | ||
| + | [[: | ||
| + | |||
| + | |||
| + | データベースの作成時、更新、削除時にはExecuteNonQuery()を使用し、 | ||
| + | 読み出しの際はExecuteReader()を使用する。 | ||
| + | |||
| + | <code powershell> | ||
| + | # sqlite3.dll を読み込む | ||
| + | $workPath = " | ||
| + | Add-Type -Path " | ||
| + | |||
| + | |||
| + | # DBのファイルパス(無ければ新規作成) | ||
| + | $dbFile = " | ||
| + | $connectionString = "Data Source=$dbFile; | ||
| + | |||
| + | if (-not (Test-Path $dbFile)) { | ||
| + | [System.Data.SQLite.SQLiteConnection]:: | ||
| + | } | ||
| + | |||
| + | # SQLite 接続オブジェクトを作成 | ||
| + | $conn = New-Object System.Data.SQLite.SQLiteConnection($connectionString) | ||
| + | $conn.Open() | ||
| + | |||
| + | # コマンドオブジェクト | ||
| + | $cmd = $conn.CreateCommand() | ||
| + | |||
| + | # テーブル作成 | ||
| + | $cmd.CommandText = @" | ||
| + | CREATE TABLE IF NOT EXISTS users ( | ||
| + | id | ||
| + | name TEXT NOT NULL, | ||
| + | age INTEGER NOT NULL, | ||
| + | email TEXT | ||
| + | ); | ||
| + | "@ | ||
| + | $cmd.ExecuteNonQuery() | ||
| + | |||
| + | Write-Host " | ||
| + | |||
| + | # サンプルデータを挿入 | ||
| + | $cmd.CommandText = " | ||
| + | $cmd.ExecuteNonQuery() | ||
| + | |||
| + | $cmd.CommandText = " | ||
| + | $cmd.ExecuteNonQuery() | ||
| + | |||
| + | Write-Host " | ||
| + | |||
| + | # データを読み出し | ||
| + | $cmd.CommandText = " | ||
| + | $reader = $cmd.ExecuteReader() | ||
| + | |||
| + | Write-Host " | ||
| + | while ($reader.Read()) { | ||
| + | Write-Host " | ||
| + | } | ||
| + | $reader.Close() | ||
| + | |||
| + | # 接続を閉じる | ||
| + | $conn.Close() | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== dll ===== | ||
| < | < | ||
| UEsDBBQAAAAAAMG8FlsAAAAAAAAAAAAAAAAEACAAeDY0L3V4CwABBAAAAAAEAAAAAFVUDQAHyoCoaCOVqGjKgKhoUEsDBBQACAAIAMG8FlsAAAAAAAAAAAAAAAAWACAAeDY0L1NRTGl0ZS5JbnRlcm9wLmRsbHV4CwABBAAAAAAEAAAAAFVUDQAHyoCoaOiAqGgAEcJe5L19eBvFtTC++rLXtuyVg52YEIhIHDA4gIsp2MiAVl7BbrImNgRIINBQgQrlo6kjJU5wHJm1wcsgGnppL+3LbXtve3u5vf2gvb2OklKiD2rLTqC2U4iT0OKElsoxlBDaxA40+54zu7YlWaHc9/ | |||