2010-06-23 304 views
2

修訂問題...請參閱下面通過VBA從Excel數據添加到MySQL

我有訪問MySQL數據庫作爲後端的Excel工作表....

我插入新的記錄MySql採用以下方式,但我不確定這是否是最好的方法。

For rowline = 1 To 50 
    strSQL = myquerystring (INSERT 5 columns per excel row)      
    rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic 
Next rowline 

基本上查詢字符串經過在Excel工作表(從1到50),每行和對某些單元中的數據被添加到SQL查詢,然後用rs.Open插入.... (每行有5列作爲記錄插入)

一切運行良好,但我只想知道是否有更快的方法(只需一個INSERT查詢),插入所有50(和5列),從第1行到第50行,全部一次。

目前它正在做50個單獨的INSERT查詢,所以我試圖將其減少到1,但我不知道它是否可能。

新信息:

你好,按照你的建議和鏈接,一些谷歌搜索,我結束了下面的代碼... 它工作正常,但是到INSERT 100行需要(謝謝!)大約15秒....這太多了。 我希望我能得到一些關於如何執行一次查詢的代碼/想法,(在一次打擊中插入所有100行)。 請注意,我是一個初學者,所以如果你可以將我引入一些樣本應該如何完成它將非常感激。

Public Sub INSERT_to_MySQL() 

    Dim conn As ADODB.Connection 
    Dim cmd As ADODB.Command 
    Dim strSQL As String 

    app_enable_false 

    On Error GoTo no_DB_connection_error 
resume_after_connecting: 

    Set cmd = New ADODB.Command 
    cmd.ActiveConnection = oConn 

    ' LOOP and INSERT the data 
    ' 100 rows take approx 15 seconds to INSERT 
    For rowcursor= 1 To 100 
       the_table = "`global`.`filesaved` " 
       strSQL = "INSERT INTO " & the_table & " (Client_Name, OriginCity, DestinationCity, ValidFrom, ValidTo, Quote_Number, Cost1, Cost2, Cost3) " 
       strSQL = strSQL & " VALUES ('" & esc(Range("BB" & rowcursor)) & "','" & esc(Range("BC" & rowcursor)) & "','" & esc(Range("BD" & rowcursor)) & "','" & Format(Range("BE" & rowcursor), "yyyy-mm-dd") & "','" & Format(Range("BF" & rowcursor), "yyyy-mm-dd") 
       strSQL = strSQL & "','" & esc(Range("BH" & rowcursor)) & "','" & esc(Range("BJ" & rowcursor)) & "','" & esc(Range("BK" & rowcursor)) & "','" & esc(Range("BJ" & rowcursor)) & "')" 
       cmd.CommandText = strSQL 
       cmd.Execute 
    Next rowcursor 

    app_enable_true 

    Exit Sub 

no_DB_connection_error: 

    ConnectDB 
    GoTo resume_after_connecting 

End Sub 
+0

可能的興趣:http:// stackoverflow。com/questions/2821718/excel-vba-writing-to-mysql-database/2821911#2821911 – Fionnuala 2010-06-23 18:36:52

+0

感謝Remou/all for yor input。 我顯然走錯了路,有很多東西要學。 – griseldas 2010-06-24 02:24:56

回答

0

如果我運行一個聲明,不要指望答覆後,我通常會在VBA的Command對象。

0

我會在循環外部移動open命令,然後使用Execute方法在循環中執行插入操作。你不想每次都打開數據庫。

這是ADO methods的好頁面。

以下是關於Batch Inserting(和更新)的問題。

編輯:在他的評論看着Remou的鏈接,我意識到你可能甚至不需要公開。只需使用插入的Execute方法即可。

1

查找下面的工作流程,以防其他人有未來相同的問題。 可能不是最好的解決方案,但是100條記錄在不到一秒的時間內一次性保存,這正是我所追求的。也只有一個INSERT查詢完成(而不是100層不同的人對每行。

感謝所有爲你的指導。

Dim conn As ADODB.Connection 
    Dim cmd As ADODB.Command 
    Dim rst_recordset As ADODB.Recordset 
    Dim strSQL As String 
    Dim strSQL2 as string 


    On Error GoTo no_DB_connection_error 
resume_after_connecting: 

    Set cmd = New ADODB.Command 
    cmd.ActiveConnection = oConn 

    ' LOOP and INSERT the data 
    lastrow = Range("BB65536").End(xlUp).Row 



       the_table = "`global`.`filesaved` " 
       strSQL = "INSERT INTO `global`.`filesaved` " & " (Client_Name, OriginCity, DestCity, ValidFrom, ValidTo, Quote_Number, Cost1, Cost2, Cost3) VALUES " 
       strSQL2 = "" 
       For excel_row = 1 To lastrow 
        strSQL2 = strSQL2 & " ('" & cells(excel_row,1) & "','" & cells(excel_row,2) & "','" & cells(excel_row,3) & "','" & cells(excel_row,4) & "','" & cells(excel_row,5) & "','" & cells(excel_row,6) & "','" & cells(excel_row,7) & "','" & cells(excel_row,8) & "','" & cells(excel_row,9) & "') ," 
       next excel_row 

       strSQL = strSQL & strSQL2 
       Mid(strSQL, Len(strSQL), 1) = ";" ' gets rid of the last comma 

       cmd.CommandText = strSQL 
       cmd.Execute 
0

此代碼爲我工作

Sub insertdata() 

Dim year As String 

Set rs = CreateObject("ADODB.Recordset") 
Database_Name = Range("b3").Value ' Name of database 
User_ID = Range("b4").Value 'id user or username 
Password = Range("b5").Value 'Password 
Server_Name =Range("b2").Value 

' Query for fetching Maximum Date 
    ' LOOP and INSERT the data 
    lastrow = Range("BB65536").End(xlUp).Row 



       the_table = "`admin`.`test` " 
       strSQL = "INSERT INTO `drawing`.`test` " & " (tests,test2) VALUES " 
       strSQL2 = "" 
       For excel_row = 1 To 100 
        strSQL2 = strSQL2 & " ('" & Cells(excel_row, 1) & "','" & Cells(excel_row, 2) & "') ," 
       Next excel_row 

       strSQL = strSQL & strSQL2 
       Mid(strSQL, Len(strSQL), 1) = ";" ' 
' ADODB connection 
Set Cn = CreateObject("ADODB.Connection") 'NEW STATEMENT 
Cn.Open "Driver={MySQL ODBC 5.1 Driver};Server=" & Server_Name & ";Database=" & Database_Name & _ 
";Uid=" & User_ID & ";Pwd=" & Password & ";" 


rs.Open strSQL, Cn, adOpenStatic 

Dim myArray() 


End Sub