阅读 83

powershell 操作sqlite数据库 增删改查(windows)

先安装
install-module sqlite
执行以下demo.ps1文件

import-module SQLite

function mount-db {
	New-PSDrive -PSProvider SQLite -Name db -Root "Data Source=file::memory:,Version=3,New=true" -Scope global | Out-Null;		
	New-Item -path db:/data -Value @{ id = ‘INTEGER PRIMARY KEY‘; username = "TEXT"; userid = "INTEGER" } | Out-Null;
		
	New-Item -path db:/data -username "USER3" -userid 3 | Out-Null
}
	
function unmount-db {		
	Remove-PSDrive -Name db | Out-Null;	
}

mount-db;
$items = ls db:/data -Filter "USERNAME=‘USER3‘";
$results = $items.userid -eq 3;
$results

set-item db:/data -Filter "USERNAME=‘USER3‘" -Value @{ userid = 1 };
$results = ( get-item db:/data/1 ).userid -eq 1;
$results

remove-item db:/data -Filter "USERNAME=‘USER3‘" 
$results = -not( get-item db:/data/1 -ErrorAction ‘silentlycontinue‘ )
$results
unmount-db;


function get-sqlitefilename {
	return ( "file:$pwd\_" + [IO.Path]::GetRandomFileName() + ‘.sqlitetest,Version=3,New=true‘ );
}
function remove-sqliteFiles {
	ls *.sqlitetest | Remove-Item -Force;
}

$filename = get-sqliteFileName;
New-PSDrive -PSProvider SQLite  -Name db -root "Data Source=$filename" | Out-Null;
# raises error when removing an nonexisting table

# adds a new table using DDL
# New-Item -Path db:/MyDb -Value @‘
# 			id INTEGER PRIMARY KEY,
# 			a1 TEXT,
# 			a2 INTEGER NOT NULL,
# 			a3 INTEGER NOT NULL,
# 			a4 INTEGER NOT NULL
# ‘@ | Out-Null;
# or  adds a new table using param slurping
# New-Item -Path db:/MyDb -id INTEGER PRIMARY KEY -a1 TEXT -a2 INTEGER NOT NULL -a3 INTEGER NOT NULL -a4 INTEGER NOT NULL | Out-Null;
# or

# creates a new record using hashtable
New-Item -Path db:/MyDb -Value @{
	id       = "INTEGER PRIMARY KEY";
	username = "TEXT";
	age      = "INTEGER";
} | Out-Null;



# Test-Path db:/mydb;
# or
$result = @( Test-Path db:/mydb );
New-Item -path db:/mydb -username "Beth" -age 39 | Out-Null;
		

$result += ( Test-Path db:/mydb/1 );
$beth = Get-Item -Path db:/mydb/1;
#$beth.age = 38;

# updates an existing record using dynamic parameters
Set-Item -Path db:/mydb/1 -age 38;
$result += ( 38 -eq ( Get-Item db:/mydb/1 ).age );

# updates an existing record using datarow object
$beth = Get-Item -Path db:/mydb/1;
$beth.age = 37;
Set-Item -Path db:/mydb/1 -value $beth;
$result += ( 37 -eq ( Get-Item db:/mydb/1 ).age );

# updates an existing record using dbnull value dynamic object
Set-Item -Path db:/mydb/1 -age $null;
$result += ( [DBNull]::Value -eq ( Get-Item db:/mydb/1 ).age );
		
New-Item -path db:/mydb -value @{ username = "Jimbo"; age = 38 }  | Out-Null;
$result += ( Test-Path db:/mydb/2 );
$result += [bool]( 2 -eq ( ls db:/mydb | measure | select -ExpandProperty count) );
$result += [bool]( ‘Jimbo‘ -eq ( gi db:/mydb/2 ).username );

# removes an existng record
Remove-Item -Path db:/mydb/2;
$result += -not( Test-Path db:/mydb/2 )

# mounts a table as a drive
New-PSDrive -PSProvider SQLite  -Name dbt -root "[Data Source=$filename]\mydb" | Out-Null;
$result += ( Test-Path dbt:/1 );
$item = Get-Item dbt:/1;
$result += $item.username -eq "Beth"

Remove-PSDrive -Name dbt | Out-Null;
Remove-PSDrive -Name db | Out-Null;

$result 

remove-sqliteFiles
# Remove-Module SQLite;

原文:https://www.cnblogs.com/whiter001/p/15226153.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐