VBA / Excel - 查询Apache Drill

时间:2017-06-22 09:52:27

标签: vba apache excel-vba apache-drill excel

我试图构建一个用于查询Apache Drill的宏。我无法让它发挥作用。

我尝试了两种方式。

首先使用QueryTables.Add。它不断添加结果,因此不会更新结果,只会将旧结果向右移动并在A1中插入新的结果。

:如何让查询删除旧结果并将新结果放在同一个地方?

第二次尝试使用ADODB.Recordset。连接不起作用

错误:

  

[Microsoft] [ODBC驱动程序管理器]未找到数据源名称且未指定默认驱动程序

:可以使用ADODB.ConnectionADODB.Recordset查询来自Appachi Drill的数据吗?

首次尝试

Sub S3Download()

Dim sConn As String
Dim oQt As QueryTable
Dim sSql As String

sConn = "ODBC;DSN=MapR Drill;"
sSql = "SQL statement"
Set oQt = Sheets("Data").QueryTables.Add(Connection:=sConn,_
         Destination:=Sheets("Data").Range("A1"), SQL:=sSql)

        With oQt
            .Name = "Query1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .Refresh BackgroundQuery:=True
            .RefreshStyle = xlInsertDeleteCells
        End With
End Sub

第二次尝试

Sub S3_download()

Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String


Dim qt As QueryTable
ThisWorkbook.Sheets("Data").Activate

ConnString = "Driver={MySQL ODBC 5.1 Driver};DSN=MapR Drill;"
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open

SQL = "SQL statement"

Set oRS = New ADODB.Recordset
oRS.Source = SQL
oRS.ActiveConnection = oCn
oRS.Open

Set qt = ThisWorkbook.Sheets("Data").ListObjects.Add(SourceType:=XlListObjectSourceType.xlSrcQuery, Source:=oRS, _
            Destination:=ThisWorkbook.Sheets("Data").Range("A1")).QueryTable

qt.Refresh

If oRS.State <> adStateClosed Then
oRS.Close
End If

If Not oRS Is Nothing Then Set oRS = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

我在第一种方式工作(宏首先清除“数据”表,然后在所需位置添加查询结果)

Sub S3Download()

    Dim sConn As String
    Dim oQt As QueryTable
    Dim sSql As String


    'removing old query data

    Sheets("Data").Cells.ClearContents

    For Each qTable In Sheets("Data").QueryTables
                qTable.Delete
    Next qTable



    sConn = "ODBC;DSN=MapR Drill;"
    sSql = "SQL statement"
    Set oQt = Sheets("Data").QueryTables.Add(Connection:=sConn,_
             Destination:=Sheets("Data").Range("A1"), SQL:=sSql)

            With oQt
                .Name = "Query1"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = False
                .RefreshPeriod = 0
                .PreserveColumnInfo = True
                .Refresh BackgroundQuery:=True
                .RefreshStyle = xlInsertDeleteCells
            End With
    End Sub