【问题标题】:SQLKata with SQLite minimal example (Powershell)SQLKata 与 SQLite 最小示例(Powershell)
【发布时间】:2021-11-06 02:40:37
【问题描述】:

我有一个 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 数据库。

谁能帮忙?

谢谢,

亚历克斯

【问题讨论】:

    标签: powershell sqlite sqlkata


    【解决方案1】:

    从 PowerShell 让这个工作受到两个困难的阻碍:

    • 加载与 NuGet 包相关的程序集,尤其是 Microsoft.Data.Sqlite NuGet 包,通常需要在 PowerShell 中进行额外的、非显而易见的工作。

    • PowerShell 通常不会像这样显示 扩展方法 - 例如.Get() 在查询实例上 - 需要显式调用 [SqlKata.Execution.QueryExtensions] 的静态方法。

    具体来说,从 PowerShell 使用 NuGet 包需要以下步骤,既不方便也不明显:

    • 仅仅使用Install-Package 安装 NuGet 包或尝试从$HOME/.nuget/packages 中的 .NET SDK 项目创建的本地缓存中使用它们通常不够,因为它们依赖于任何程序集 on 不会出现在同一目录中,这是 Add-Type 所要求的。

    • 它们还必须通过辅助 .NET SDK 项目以适合平台的方式解包到单个目标文件夹(每个包或组合),如this answer 中所述。

    • 另外,对于Microsoft.Data.Sqlite 包,适合平台的本机 库(例如,来自.NET SDK 的“runtimes”文件夹子树的win-x64\native\*.dll项目的发布文件夹)必须直接复制到 PowerShell(核心)中的目标文件夹,但奇怪的是在 Windows PowerShell 中不复制,至少从包版本 5.0.9 开始


    以下示例代码使用Add-NuGetType 辅助函数,可从this MIT-licensed Gist 获得,它自动执行上述所有步骤:

    注意:

    • 假设您已经查看了链接代码以确保它是安全的(我可以亲自向您保证,但您应该始终检查),您可以直接安装Add-NuGetType,如下所示(有关如何制作函数的说明将显示在以后的会话中可用或将其转换为脚本):

      irm https://gist.github.com/mklement0/7436c9e4b2f73d7256498f959f0d5a7c/raw/Add-NuGetType.ps1 | iex
      
    • 第一次运行时,该函数会下载并安装 .NET SDK 的私有副本,该副本嵌入在缓存后来下载的 NuGet 包的文件夹中。此初始安装需要一段时间,下面使用的-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] instances
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-08
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2014-12-12
      • 2020-10-14
      相关资源
      最近更新 更多