Excel 中使用 SQLite ODBC 的参数化查询

parameterized queries in Excel with SQLite ODBC

我在 Excel 2016 (win10) 中使用 SQLite ODBC Driver v0.9993。使用外部资源的主要推动力是我有太多数据需要 Excel 进行合理管理,因此将根据用户 select 编辑的标准提取数据。因此,我想使用基于明确定义的工作表单元格内容的参数化查询。

我正在尝试两种方法:

  1. Straight VBA,我在这里做了这样的事情(粗略的代码):

    Sub UpdateTables()
        Dim ws as Worksheet
        Dim adoCN As ADODB.Connection
        Dim adoCmd As ADODB.Command
        Dim adoRS As ADODB.Recordset
        Dim sDB as String
        Dim rCell as Range
        Set adoCN = New ADODB.Connection
        Set adoRS = New ADODB.Recordset
        ' ws is set to correct worksheet
        ' ...
        ' define sDB from worksheet cell
        With adoCN
            .Open "Provider=MSDASQL.1;Persist Security Info=True;" _
                & "Driver={SQLite3 ODBC Driver};" _
                & "Database=" & sDB & ";" _
                & "DSN=SQLite3 Datasource;LongNames=true;fksupport=true", "", "" '
        End With
        Set adoCmd = New ADODB.Command
        adoCmd.ActiveConnection = adoCN
        ' rCell points to cell containing query parameter
        Set adoParam = adoCmd.CreateParameter(, adVarChar, adParamInput, _
            Len(rCell.value), rCell.value)
        adoCmd.Parameters.Append adoParam
        adoCmd.CommandText = "SELECT * FROM TableName WHERE X = ?"
        adoRS.Open Source:=adoCmd, CursorType:=adOpenKeyset
        With ws.ListObjects(1).QueryTable
            Set .RecordSet = adoRS
            .Refresh ' errors with "Error 1004: invalid accessor flag"
        End With
    End Sub
    

    (代码已经简化,通常我包括完整性检查。)

  2. 在 Excel 中基于 GUI,使用 新查询 > 来自其他来源 > 从 ODBC,将 DSN 设置为 "SQLite3 Datasource",然后输入上面使用的连接字符串。

    不幸的是,"Parameters" 按钮 (Connections > select query > Properties > 定义 选项卡)变灰。

我想我更喜欢第二种解决方案,但目前都不起作用。

而不是 opening recordset via an ADO connection, you need to execute the command from ADO command 对象。这是 recordset .execute 与 .open 经常讨论的话题。当然一定要加上错误处理捕获相关的errors/exceptions.

Sub UpdateTables()
On Error GoTo ErrHandle
    Dim ws as Worksheet
    Dim adoCN As New ADODB.Connection, adoRS As New ADODB.Recordset
    Dim adoCmd As New ADODB.Command    
    Dim sDB as String
    Dim rCell as Range

    'Set ws = ... '
    'sDB = ... '

    ' DATABASE CONNECTION '   
    adoCN.Open "Provider=MSDASQL.1;Persist Security Info=True;" _
                  & "Driver={SQLite3 ODBC Driver};" _
                  & "Database=" & sDB & ";" _
                  & "DSN=SQLite3 Datasource;LongNames=true;fksupport=true", "", "" 

    ' ADO COMMAND '
    With adoCmd
        .ActiveConnection = adoCN
        .CommandText = "SELECT * FROM TableName WHERE X = ?"
        .CommandType = adCmdText
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, _
                                            Len(rCell.value), rCell.value)
    End With

    ' EXECUTE RECORDSET '
    Set adoRS = adoCmd.Execute

    ' DEFINE QUERYTABLE '
    With ws.ListObjects(1).QueryTable
        Set .RecordSet = adoRS
        .Refresh
    End With

    ' CLOSE AND FREE RESOURCES '
    adoRS.Close: adoCN.Close
    Set adoRS = Nothing: Set adoCmd = Nothing: Set adoCN = Nothing    
    Exit Sub

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description
    Set adoRS = Nothing: Set adCmd = Nothing: Set adCN = Nothing    
    Exit Sub    
End Sub