从Excel批量插入到没有TXT或CSV文件的SQLite

我有一个问题,我希望得到任何你的意见。

基本上,我需要在VBA中编写一个代码,使我能够从excel到sqlite的数据表。

我设法做到这一点使用此代码:

Sub Scrivi() Dim objConnection As Object, objRecordset As Object Dim strDatabase As String Set objConnection = CreateObject("ADODB.Connection") strDatabase = "DRIVER={SQLite3 ODBC Driver};Database=" & Application.ActiveWorkbook.Path & "\TestDB.db;" Query = "drop table FUND; create table FUND (Fund_Code, Fund_Name, End_Date, PRIMARY KEY('Fund_Code'));" riga = 3 colonna = 1 While Not (IsNull(ActiveSheet.Cells(row, column).Value) Or IsEmpty(ActiveSheet.Cells(row, column).Value)) Query = Query & "insert into FUND values('" & Cells(row, column) & "','" & Cells(row, column + 1) & "','" & Cells(row, column + 2) & "');" row = row + 1 Wend objConnection.Open strDatabase objConnection.Execute Query objConnection.Close End Sub 

现在我想改变迭代的一部分,我说row = row + 1的一种批量插入,其中程序采取整个表,而不是逐行。

如果可能,我不想创buildcsv / txt文件。

非常感谢,任何意见将不胜感激。

一个非常基本的解决scheme可以通过首先构build批量插入的完整查询,然后一次执行查询。

例如 –

插入FUND值(val1,val2); 插入FUND值(val3,val4);

可以用 – 插入到FUND值(val1,val2),(val3,val4)

你可以将所有的数据从worksheet.range附加到数组(变体)

 Dim arr as Variant arr = Application.Transpose(range(your_range_of_data).values) 

然后,使用数组中的数据,使用此模式构buildSQL查询string

 INSERT INTO table (column1, column2, ... ) VALUES (expression1, expression2, ... ), (expression1, expression2, ... ), ...; 

VBAfunction(写在“膝盖上”,所以它可能需要一点点调整,对不起)

 Function buildQueryFromArray(arr as Variant, table as String, columns as String) as String Dim strSqlQuery as String * 4096 Dim i as Integer strSqlQuery = "INSERT INTO " & table & " (" & columns & ") VALUES" For i=LBound(arr, 1) to UBound(arr, 1) strSqlQuery = strSqlQuery & "(" & arr(i,1) & ", " & arr(i,2) & "etc" & ")," Next i buildQueryFromArray = Left(strSqlQuery, len(strSqlQuery)-1) & ";" End Function 

使用数组而不是直接从工作表中获取数据的原因是:它的速度要快得多。 你可以在这里阅读更多: http: //www.cpearson.com/excel/ArraysAndRanges.aspx lemme知道,如果这对你有用:)


编辑:
Ad1:你是对的,我们其实不想转置它。 不过,我习惯于这样写,因为在本主题的第7篇文章中已经很好地描述了这些原因: mrexcel.com/forum/为什么我们使用application.transpose?
Ad2:不,如果这是你的问题,它不必在function,但它使代码更清洁,更可重用。 以下您可以find内联解决scheme:

 Sub Scrivi() Dim objConnection As Object, objRecordset As Object Dim strDatabase As String, strQuery as String Set objConnection = CreateObject("ADODB.Connection") strDatabase = "DRIVER={SQLite3 ODBC Driver};Database=" & Application.ActiveWorkbook.Path & "\TestDB.db;" strQuery = "drop table FUND; create table FUND (Fund_Code, Fund_Name, End_Date, PRIMARY KEY('Fund_Code')); insert into FUND values" 'determine how many rows of data there is Dim lastRow as Integer lastRow = ActiveSheet.Range("A:A").Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 'transfer all data from range to array 'range: from A3 to Cx, where x equals to lastRow Dim arr as Variant arr = ActiveSheet.Range("A3:C" & lastRow).Value 'add all records from arr into single SQL query 'Lbound(xArray, xDimension)/Ubound(xArray, xDimension) - returns integer equal to first/last xArray item in xDimension. Everyday-use function. Google some more to have better insight of whats going on Dim i as Integer For i=Lbound(arr,1) to Ubound(arr,1) strQuery = strQuery & "(" & arr(i,1) & "," & _ arr(i,2) & "," & _ arr(i,3) & ")," Next i 'from the strQuery - swap last comma for semi-colon strQuery = Left(strQuery, len(strQuery)-1) & ";" objConnection.Open strDatabase objConnection.Execute strQuery objConnection.Close 

让我知道一切是否清楚:)