我有一个sqlite数据库,比如c:\myDb.sqlite
我已经知道了如何在SQLKata中构建对这个数据库的查询:
$query = New-Object SqlKata.Query("myTable")
$compiler = New-Object SqlKata.Compilers.SqliteCompiler
$query.Where("myColumn", "1")
$result = $compiler.Compile($query)但我一点也不知道如何把这个提交给我的Sqlite数据库。
有人能帮忙吗?
谢谢,
亚历克斯
发布于 2021-09-10 02:54:44
要从PowerShell中实现这一目标受到了两个困难的阻碍:
Microsoft.Data.Sqlite NuGet包,通常需要在PowerShell中进行额外的、不明显的工作。.Get() --而是需要显式调用[SqlKata.Execution.QueryExtensions]的静态方法。具体来说,使用来自PowerShell的PowerShell包需要以下步骤,这些步骤既不方便,也不明显:
Install-Package安装Install-Package包或尝试从$HOME/.nuget/packages中的.NET SDK项目创建的本地缓存中使用它们通常是不够的,因为它们所依赖的任何程序集都不存在于同一个目录中,这正是Add-Type所需要的。Microsoft.Data.Sqlite包,平台相关的本机库(例如,来自.NET SDK项目发布文件夹的“运行时”文件夹子树的win-x64\native\*.dll )必须直接复制到PowerShell (.NET)中的目标文件夹,但奇怪的是,至少在5.0.9包中没有复制到PowerShell PowerShell中。下面的Add-NuGetType示例代码使用了从这个麻省理工学院授权的吉斯特获得的助手函数,该函数自动执行上述所有步骤:
注意:
Add-NuGetType,如下所示(将显示关于如何在以后的会话中使函数可用或如何将其转换为脚本的说明):
irm https://gist.github.com/mklement0/7436c9e4b2f73d7256498f959f0d5a7c/raw/Add-NuGetType.ps1 \ iex-Verbose开关报告了它的进度。Add-NuGetType不是用于生产,而是用于试验NuGet包;运行help Add-NuGetType获取更多信息。# Reference the relevant namespaces.
using namespace SqlKata
using namespace SqlKata.Compilers
using namespace SqlKata.Execution
using namespace Microsoft.Data.Sqlite
# Load the SqlKata and Sqlite asssemblies.
# See the comments above for how to install the Add-NuGetType function.
# Note: On first call, a private copy of the .NET SDK is downloaded
# on demand, which takes a while.
Add-NuGetType -Verbose SqlKata, SqlKata.Execution, Microsoft.Data.Sqlite
# First, create sample database './sample.db' with table 'sample_table'
@'
create table sample_table (Name string, Age int);
insert into sample_table (Name, Age) values ("JDoe", 42), ("JRoe", 43);
.save ./sample.db
'@ | sqlite3
# Create a [SqliteConnection] instance...
$connection = [SqliteConnection]::new("Data Source=$PWD/sample.db")
# ... and create a query factory for it.
$sqliteDb = [QueryFactory]::new($connection, [SqlServerCompiler]::new())
# Create and execute a sample query.
$query = $sqliteDb.Query("sample_table").Where("Name", "JRoe")
# Note the need to use the static methods of [SqlKata.Execution.QueryExtensions],
# because PowerShell doesn't make *extension methods* automatically available.
[SqlKata.Execution.QueryExtensions]::Get($query) # outputs [Dapper.SqlMapper+DapperRow] instanceshttps://stackoverflow.com/questions/69118045
复制相似问题