ユーザ用ツール

サイト用ツール


powershell:sqlite_create_table

SQLiteでテーブルを作成する

一つ上へ

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;"
 
# SQLite 接続オブジェクトを作成
$conn = New-Object System.Data.SQLite.SQLiteConnection($connStr)
$conn.Open()
 
if (-not (Test-Path $dbFile)) {
    [System.Data.SQLite.SQLiteConnection]::CreateFile($dbFile)
}
 
# コマンドオブジェクト
$cmd = $conn.CreateCommand()
 
# 初期化処理
# テーブルが無ければ作成する
$cmd.CommandText = @"
CREATE TABLE IF NOT EXISTS tablename (
    date   TEXT NOT NULL,
    col1   TEXT,
    col2 TEXT
);
"@
 
$cmd.ExecuteNonQuery()
 
$conn.Close()
powershell/sqlite_create_table.txt · 最終更新: 2025/10/14 00:45 by mikoto