18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShell で CSV に SQL(Select文)を投げつける

Last updated at Posted at 2015-10-06

q コマンドをインストール出来ないネットから隔絶された PC で、CSV ファイルに SQL を投げたかったので振休使って調べました。q コマンドについては下記参照。

実装はCodeの節参照。

使ってみる

データ

文字コードはUTF-8

No,A,I,U
1,あ,い,う
2,か,き,く
3,さ,し,す
No,E,O
1,え,お
3,せ,そ

投げつける Select 文

@(
  "select * from R.csv",
  "select * from S.csv",
  "select R.No, A, I, U, E, O from R.csv R inner join S.csv S on R.No = S.No"
) | %{$_ | Select-Csv | Format-Table}

結果

No A I U
--- - - -
  1 あ い う
  2 か き く
  3 さ し す

No E O
--- - -
  1 え お
  3 せ そ

No A I U E O
-- - - - - -
 1 あ い う え お
 3 さ し す せ そ

参考にしたサイトとか

Code

function Select-Csv
{
  Param
  (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)] 
    [String] $Query,

    [ValidateNotNullOrEmpty()]
    [String] $Path = (Get-Location).Path,

    # [Text.Encoding]::GetEncodings() | % Name
    [ValidateSet('UTF-8', 'Shift_JIS')]
    [String] $Encoding = 'UTF-8',

    [ValidateNotNull()]
    [Switch] $NoHead
  )

  Begin
  {
    $close = {
      if ($Connection -ne $null)
      {
        $Connection.Close()
        $Connection.Dispose()
      }
    }

    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Data")
    $Connection  = New-Object 'System.Data.OleDb.OleDbConnection'
    $Cmd         = New-Object 'System.Data.OleDb.OleDbCommand'
    $DataAdapter = New-Object 'System.Data.OleDb.OleDbDataAdapter'
    $DataSet     = New-Object 'System.Data.DataSet'

    $Path = Resolve-Path $Path
    $HDR = if ($NoHead) {'NO'} else {'YES'}
    $CharacterSet = [Text.Encoding]::GetEncoding($Encoding).CodePage

    $ExtendedProperties = @(
      "Text"
      "HDR=${HDR}"
      "FMT=Delimited"
      "CharacterSet=${CharacterSet}"
    ) -join ";"

    $Connection.ConnectionString = @(
      "Provider=Microsoft.Jet.OleDb.4.0",
      "Data Source=${Path}",
      "Extended Properties=`"${ExtendedProperties}`""
    ) -join ";"

    trap {& $close; break}
  }

  Process
  {  
    $DataSet.Tables.Clear()

    $Cmd.CommandText = $Query
    $Cmd.Connection  = $Connection 

    $DataAdapter.SelectCommand = $Cmd 
    $DataAdapter.Fill($DataSet) | Out-Null

    $DataSet.Tables[0]

    trap {& $close; break}
  }

  End {& $close}
}
18
19
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
18
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?