PowerShellでデータベース(Mdb)を操作できるモジュールをGitHubに公開しました。
こんにちは。
powershellで、データベースをよく使っているので、気合を入れてモジュールにまとめました。
Windowsに何もインストールしなくても使えます。
mdbファイルを操作するのに、MS OfficeのAccessは不要です。さすがに、あったら便利ですが。
モジュールは以下で、ダウンロードできます。
◆rancomma / MdbCommand
GitHub - rancomma/MdbCommand: powershell ms mdb file automation command.
モジュールについては、powershellに、「man about_modules」と入力するか、
以下のリンクを見てください。(同じ内容です)
◆MS TechNet about_modules
about_Modules | Microsoft Docs
使い方
使用するコマンドは、最初は4つです。
| コマンド | 内容 |
|---|---|
| ipmo | モジュールをインポートします。ipmoは、Import-Moduleのエイリアスです。 |
| open-mdb | mdbファイルをオープンします。mdbファイルの作成もできます。 |
| invoke-mdb | SQLを実行します。 |
| close-mdb | mdbファイルをクローズします。 |
| get-mdb | オープンしているmdbファイルを確認します。 |
インポートします。
モジュールフォルダにコピーした場合
PS (1) > ipmo MdbCommand
どこかのフォルダにコピーした場合
PS (1) > ipmo c:\ps1\anyfolder\MdbCommand
相対パスでも大丈夫です。
PS (1) > ipmo .\anyfolder\MdbCommand
ファイルを作成してオープンします。
PS (2) > open-mdb sample.mdb -create
テーブルを作成します。
PS (3) > invoke-mdb "create table test (a int, b string)"
データを登録します。
PS (4) > invoke-mdb "insert into test (a, b) values (1, '文字列')" PS (5) > invoke-mdb "insert into test (a, b) values (2, 'あいう')" PS (6) > invoke-mdb "insert into test (a, b) values (3, 'abc')"
データを参照します。
PS (7) > invoke-mdb "select * from test"
a b
- -
1 文字列
2 あいう
3 abcこんな感じで返ってきます。
ちょっとみずらいので、自動幅調整して、参照します。
PS (8) > invoke-mdb "select * from test" | ft -auto a b - - 1 文字列 2 あいう 3 abc
開いているMdbファイルの情報を取得します。
PS (9) > get-mdb DatabaseName State Transaction DataSource ------------ ----- ----------- ---------- main Open No C:\scripts\ps1\sample.mdb
Mdbファイルを閉じます。
PS (10) > close-mdb
その他の操作は、GitHubにおいてあるREADMEファイルを見てください。
転載しておきます。
使ってみてください!
README
[Install Guide]
1. Check PSModulePath
C:\PS>$env:PSModulePath -split ";"
C:\Users\UserName\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
2. Copy MdbCommand Folder
C:\Users\UserName\Documents\WindowsPowerShell\Modules\MdbCommand
[MdbCommand Exsamples]
C:\PS>ipmo MdbCommand
C:\PS>Open-Mdb newsample.mdb -Create
C:\PS>Invoke-Mdb 'Create Table table1 (a int not null constraint PK Primary Key, b varchar,
c yesno)'
C:\PS>invoke-mdb 'create table table2 (ID counter not null primary key, a int, b int)'
C:\PS>invoke-mdb @'
create table table3
(
col1 string(5),
col2 string(5),
a int,
constraint PK_table3 primary key (col1, col2)
)
'@
C:\PS>Get-Mdb
DatabaseName State Transaction DataSource
------------ ----- ----------- ----------
main Open No C:\scripts\ps1\newsample.mdb
C:\PS>Get-MdbTable
TableName TableType Description
--------- --------- -----------
table1 TABLE
table2 TABLE
table3 TABLE
C:\PS>Get-MdbColumn -TableName table1|ft -auto
TableName Pos ColumnName DataType AliasType Size NotNull Default PKey AutoInc
--------- --- ---------- -------- --------- ---- ------- ------- ---- -------
table1 1 a INTEGER int YES YES
table1 2 b VARCHAR string 255
table1 3 c BIT yesno YES
C:\PS>Get-MdbIndex |ft -auto
TableName IndexName Primary Unique Position ColumnName Collation
--------- --------- ------- ------ -------- ---------- ---------
table1 PK YES YES 1 a ASC
table2 Index_7BC68A99_2CED_49DE YES YES 1 ID ASC
table3 PK_table3 YES YES 1 col1 ASC
table3 PK_table3 YES YES 2 col2 ASC
C:\PS>$list = new-object collections.arraylist
C:\PS>$list.add(@(1,'colb1', $true))
C:\PS>$list.add(@(2,'colb2', $false))
C:\PS>$list.add(@(3,'colb3', $true))
C:\PS>Invoke-MdbBatch 'Insert Into table1 (a, b, c) Values (?, ?, ?)' $list
C:\PS>${MdbCommand.BatchTotalRecordsAffected}
3
C:\PS>Add-MdbQuery InsertTable1 'Insert Into Table1 (a, b, c) Values (?, ?, ?)'
C:\PS>Get-MdbQuery | ft -wrap
QueryName QueryType SQL
--------- --------- ---
InsertTable1 ACTION INSERT INTO Table1 ( a, b, c )
C:\PS>Invoke-MdbQuery InsertTable1 -Value 4,'colb4',$false
C:\PS>Invoke-Mdb 'Select a, b, c From table1 Where a > ?' 1 | ft -auto
a b c
- - -
2 colb2 False
3 colb3 True
4 colb4 False
C:\PS>try
>> {
>> Start-MdbTransaction
>>
>> Invoke-Mdb "Update table1 Set b = 'colb_change' Where a = 1"
>>
>> # processing
>>
>> Complete-MdbTransaction
>> }
>> catch
>> {
>> Write-Warning "$_"
>> Undo-MdbTransaction
>> }
C:\PS>Start-MdbConsole
mdb> select * from table1
...> /
a b c
- - -
1 colb1 True
2 colb2 False
3 colb3 True
4 colb4 False
mdb> .show
autosize : on
echo : off
headers : on
nullvalue : ""
width :
wrap : on
mdb>
mdb> .help
.autosize ON|OFF Like Format-Table AutoSize parameter
.begin Start transaction
.columns ?TABLE? List columns
.commit Commit transaction
.databases Show database Info
.echo ON|OFF Turn command echo on or off
.exit Exit this console
.headers ON|OFF Turn display of headers on or off
.help Show this message
.indices ?T? ?C? ?I? List indices
.nullvalue STRING Print STRING in place of NULL values
.quit Exit this console
.rollback Rollback transaction
.show Show the current values for various settings
.tables ?TABLE? List tables
.views ?VIEW? List querys and procedures
.width NUM1 NUM2 ... Set column widths (If ".width" Then clear widths)
.wrap ON|OFF Like Format-Table Wrap parameter
; or / or GO Enter SQL statements terminated
mdb>
mdb> .quit
C:\PS>
C:\PS>Use-MdbAliasCommand -Prefix mdb-
C:\PS>get-alias mdb-*
CommandType Name Definition
----------- ---- ----------
Alias mdb-begin Start-MdbTransaction
Alias mdb-close Close-Mdb
Alias mdb-columns Get-MdbColumn
.....................................
C:\PS>Use-MdbDotCommand
C:\PS>get-alias .*
CommandType Name Definition
----------- ---- ----------
Alias .begin Start-MdbTransaction
Alias .close Close-Mdb
Alias .columns Get-MdbColumn
Alias .commit Complete-MdbTransaction
Alias .console Start-MdbConsole
.....................................
C:\PS>Close-Mdb
C:\PS>.mdb sample1.mdb
C:\PS>.databases
C:\PS>.columns
C:\PS>.querys
C:\PS>.execute 'select * from table1'
C:\PS>.quit