VBA/Excel - 查询 Apache Drill
VBA/Excel - querying Apache Drill
我正在尝试构建一个用于查询 Apache Drill 的宏。我无法让它工作。
我试了两种方法。
首先使用QueryTables.Add
。它不断添加结果,这样就不会更新结果,而只会将旧结果向右移动并在 A1 中插入新结果集。
Q: 如何让查询删除旧的结果并将新的结果放在同一个地方?
第二次尝试使用 ADODB.Recordset
。连接不工作
错误:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Q:ADODB.Connection
,ADODB.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
我解决了第一种方法(宏首先清除 "Data" sheet,然后将查询结果添加到所需位置)
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
我正在尝试构建一个用于查询 Apache Drill 的宏。我无法让它工作。
我试了两种方法。
首先使用QueryTables.Add
。它不断添加结果,这样就不会更新结果,而只会将旧结果向右移动并在 A1 中插入新结果集。
Q: 如何让查询删除旧的结果并将新的结果放在同一个地方?
第二次尝试使用 ADODB.Recordset
。连接不工作
错误:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Q:ADODB.Connection
,ADODB.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
我解决了第一种方法(宏首先清除 "Data" sheet,然后将查询结果添加到所需位置)
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