VBA/Excel-查询apachedrill
我试图构建一个宏来查询apachedrill。我不能让它工作 我试了两种方法 首先使用VBA/Excel-查询apachedrill,vba,apache,excel,apache-drill,Vba,Apache,Excel,Apache Drill,我试图构建一个宏来查询apachedrill。我不能让它工作 我试了两种方法 首先使用查询表。添加。它不断添加结果,这样就不会更新结果,只会将旧结果向右移动,并在A1中插入新的集合 Q:如何使查询删除旧结果并将新结果放在同一位置 第二次尝试使用ADODB.Recordset。连接不工作 错误: [Microsoft][ODBC驱动程序管理器]未找到数据源名称,也未指定默认驱动程序 Q:能否使用ADODB.Connection,ADODB.Recordset从Appachi Drill查询数据 第
查询表。添加。它不断添加结果,这样就不会更新结果,只会将旧结果向右移动,并在A1中插入新的集合
Q:如何使查询删除旧结果并将新结果放在同一位置
第二次尝试使用ADODB.Recordset
。连接不工作
错误:
[Microsoft][ODBC驱动程序管理器]未找到数据源名称,也未指定默认驱动程序
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
第二次尝试
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
Sub S3_下载()
作为ADODB.连接的Dim oCn
将oRS设置为ADODB.Recordset
将字符串变为字符串
将SQL设置为字符串
将qt设置为查询表
此工作簿。工作表(“数据”)。激活
ConnString=“Driver={MySQL ODBC 5.1 Driver};DSN=MapR Drill;”
设置oCn=New ADODB.Connection
oCn.ConnectionString=ConnString
开放式
SQL=“SQL语句”
Set oRS=新的ADODB.Recordset
oRS.Source=SQL
oRS.ActiveConnection=oCn
开放的
设置qt=thiswook.Sheets(“数据”).ListObjects.Add(SourceType:=XlListObjectSourceType.xlSrcQuery,Source:=oRS_
目标:=ThisWorkbook.Sheets(“数据”).Range(“A1”).QueryTable
qt.刷新
如果oRS.State已关闭,则
好的,结束
如果结束
如果不是oRS,则设置oRS=Nothing
如果不是oCn为Nothing,则设置oCn=Nothing
端接头
我采用了第一种方法(宏首先清除“数据”表,然后将查询结果添加到所需位置)