powershell玩转sqlite数据库详细介绍

2019-09-30 09:33:49于海丽

SQLite Developer http://www.sqlitedeveloper.com/
SQLiteSpy http://www.yunqa.de/delphi/doku.php/products/sqlitespy/index
RazorSQL http://www.razorsql.com/

中文:
SQLite Administrator http://sqliteadmin.orbmu2k.de/ 打不开3.x版本数据库
navicat for sqlite //www.jb51.net/database/132499.html推荐
Database.NET http://fishcodelib.com/database.htm 推荐

------------------【下载安装】------------------
ps调用sqlite的两种方法。这里我们主要介绍第二种
1 https://psqlite.codeplex.com/
2 https://github.com/RamblingCookieMonster/PSSQLite
上述2种,都支持支持winxp,powershell2.0,及以上。

2.1 下载:
https://github.com/RamblingCookieMonster/PSSQLite/archive/master.zip

易采站长站下载:PSSQLite-master-jb51.zip

2.2 建立【C:Users你的用户名DocumentsWindowsPowerShellModules】目录,并解压:
把 PSSQLite-masterPSSQLite 这个目录复制到,你的模块目录中。
即modules目录中,含有PSSQLite这个目录。

2.3 查看模块中的所有命令:
Import-Module pssqlite
get-command * -module pssqlite

返回
CommandType Name Version Source
----------- ---- ------- ------
Function Invoke-SQLiteBulkCopy 1.0.1 pssqlite
Function Invoke-SqliteQuery 1.0.1 pssqlite
Function New-SQLiteConnection 1.0.1 pssqlite
Function Out-DataTable 1.0.1 pssqlite

2.4 一键安装命令:
#一键安装命令依赖.net 4.5 及以上。powershell 3.0 及以上。如果不满足条件,请用手动安装方法。
mkdir "$env:USERPROFILEDocumentsWindowsPowerShellModules"
Invoke-WebRequest https://github.com/RamblingCookieMonster/PSSQLite/archive/master.zip -OutFile .master.zip
add-Type -AssemblyName 'System.IO.Compression.Filesystem'
[System.IO.Compression.ZipFile]::ExtractToDirectory("$pwdmaster.zip","$pwd")
Copy-Item -Recurse PSSQLite-masterPSSQLite "$env:USERPROFILEDocumentsWindowsPowerShellModules"
Import-Module pssqlite
get-command * -module pssqlite

------------------【命令帮助】------------------

Invoke-SqliteQuery -DataSource $库文件名 -query $查询字符串 -as 输出格式(PSObject,DataRow,DataTable,DataSet,SingleValue)


$内存库 = New-SQLiteConnection -DataSource :MEMORY:
Invoke-SqliteQuery -SQLiteConnection $内存库 -Query "CREATE TABLE OrdersToNames (OrderID INT PRIMARY KEY, fullname TEXT);" #建表语句
Invoke-SqliteQuery -SQLiteConnection $内存库 -Query "INSERT INTO OrdersToNames (OrderID, fullname) VALUES (1,'Cookie Monster');" #插入语句
Invoke-SqliteQuery -SQLiteConnection $内存库 -Query "SELECT xxx FROM yyy WHERE mmm=nnn;" #你想要的统计语句

问:csv如何入库?
答:
最好用图形工具搞,也可以用下列类似命令。
$dt1 = get-process | Out-DataTable
Invoke-SQLiteBulkCopy -DataTable $st1 -DataSource $DataSource -Table 表名