0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Sqliteファイルの再作成.ps1

Posted at

1. スクリプト概要

スクリプト名: Sqliteファイルの再作成.ps1
SQLiteファイルをエクスポート・インポートすることで、新しいSQLiteファイルを再作成します。

2. 処理と目的

処理の流れ
 1. 再作成するSQLiteファイルをフルパスで指定します
 2. 手順1で指定したファイルを一時フォルダにコピーします
 3. エクスポートコマンドを実行し、一時フォルダにエクスポートデータファイルを作成します
 4. インポートコマンドを実行し、一時フォルダに新規のSQLiteファイルを作成します
 5. 一時フォルダをエクスプローラで開きます

目的
 SQLiteファイルのデータを大量に書き換えていると、ファイルサイズ肥大化することがあり、その対応として作成したスクリプトです

3. 動作環境と要件

PowerShellのバージョン
7.0以上

OS
Windows10

必要なモジュール
sqlite-tools

必要な権限
特になし

その他の設定
特になし

4. 使用方法

基本的な実行方法
スクリプトコードを拡張子ps1で保存してPowershellで実行してください。
ファイルを保存する際は、文字コードをUTF8 BOM付にしてください。
スクリプトコード内の$sqlite3のパスをご自身がインストールしたパスに変更してください

パラメータ
なし

使用例

  1. コマンドラインでpwsh Sqliteファイルの再作成.ps1を実行
  2. 処理対象のSQLiteファイルを聞いてくるのでファイルのフルパスを入力

5. スクリプトコード

$host.UI.RawUI.WindowTitle = ([IO.Path]::GetFilenameWithoutExtension($PSCommandPath))
$ErrorActionPreference = 'Stop'

$sqlite3 = "D:\DB\sqlite-tools-win32-x86-3310100\sqlite3.exe"

Write-Host @"
--------------------------------------------------------------------------
指定したSQLITEファイルを
「${sqlite3}」
を使用して再作成します。
--------------------------------------------------------------------------
"@ -ForegroundColor Black -BackgroundColor Gray


do {
  #$dbfile = "D:\My_Folder\powershellBatch\iwara\iwaraVideo.sqlite"
  $dbfile = Read-Host "再作成するSqliteファイルを指定してください。"
  $dbfile = $dbfile.trim('"')
} while( -not ([IO.File]::Exists($dbfile)) )

$exportCommand = ('.output ./dump.txt ','.dump','.exit')
$importCommand = (".read ./dump.txt",'.exit' )

do {
    $tempDir = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName())
} while(Test-Path -LiteralPath $tempDir)
echo "一時フォルダ「${tempDir}」を作成"
$null = mkdir $tempDir
Set-location -LiteralPath $tempDir

$tempdbfile = Join-Path $tempDir ([IO.Path]::GetFileName($dbfile))
echo "一時ファイル「${tempdbfile}」にファイルコピー開始"
copy -LiteralPath $dbfile $tempdbfile


echo "DBファイルのエクスポート開始"
$exportCommand | & $sqlite3 $tempdbfile
if($lastexitcode -ne 0){
  Write-Warning "エクスポートでエラーが発生しました。終了します"
  pause
  return
}

do {
    $newdbfile = Join-Path $tempDir (([IO.Path]::GetRandomFileName())+([IO.Path]::GetExtension($dbfile)))
} while(Test-Path -LiteralPath $newdbfile)


echo "新DBファイル「${newdbfile}」へのインポート開始"
$importCommand| & $sqlite3 $newdbfile
if($lastexitcode -ne 0){
  Write-Warning "インポートでエラーが発生しました。終了します"
  pause
  return
}

explorer $tempDir

pause

6. 注意事項と既知の問題

制約事項
サイズの大きいファイルを処理する場合、完了までに時間がかかる可能性があります。

既知のバグ
もしバグを発見された場合は、コメントでご報告ください。

トラブルシューティング
・ps1ファイルのエンコーディングには注意してください。
・PowerShell 7未満では動きません

7. 免責事項

本スクリプトにはいかなる保証もありません。使用は自己責任で行ってください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?