【SQLite】SQLite3 データを HTML で出力する (Windows10 環境)【SW】

SQLite3 データを HTML で出力する (Windows10 環境)
 
PowerShell を使って SQLite3 DB の内容を HTML として出力する。
先にシェルスクリプト版を作り、その後、Windows環境用に PowerShell で書き換えたものである。
本ページにもシェルスクリプト版を転記しておく。

目次

[隠す]

 

PowerShell 版

 
sqlite3_html.ps1

$sqlitedb = $args[0]
Write-Output '<html><body>'

foreach ( $i in ( sqlite3 $DB ".tables" ).Split() )
{
  if( [string]::IsNullOrEmpty($i) )
  {
    continue
  }
  Write-Output "<h2>$i</h2>"
  Write-Output "<table border=1>"
  Write-Output "<tr>"

  foreach ( $j in ( sqlite3 $DB "PRAGMA table_info($i);" ))
  {
    if( [string]::IsNullOrEmpty($j) )
    {
      continue
    }
    $j = $j.Split('|')[1]
    Write-Output "<th>$j</th>"
  }
  Write-Output "</tr>"

  sqlite3 -html $DB "select * from $i ;"
  Write-Output "</table>"
}
Write-Output "</body></html>"

 

シェルスクリプト版

#!/bin/sh
sqlitedb=$1

echo "<html><body>"
for i in `sqlite3 ${sqlitedb} ".tables"`
do 
  echo "<h2>${i}</h2>"
  echo "<table border=1>"
  echo "<tr>"
  for j in `sqlite3 ${sqlitedb} "PRAGMA table_info(${i});" | awk 'BEGIN{FS="|"}{printf "%s\n", $2}'`
  do 
    echo "<th>${j}</th>"
  done
  echo "</tr>"
  sqlite3 -html ${sqlitedb} "select * from ${i};"
  echo "</table>"
done
echo "</body></html>"

スクリプトの使用方法

> .\sqlite3_html.ps1  [DataBase] > output.html