通过VBA将Excel中的数据添加到Mysql

通过VBA将Excel中的数据添加到Mysql,mysql,excel,vba,insert,Mysql,Excel,Vba,Insert,更新的问题…见下文 我有一个excel工作表,可以访问MySQL数据库作为后端 我用以下方法将新记录插入MySql,但我不确定这是否是最好的方法 For rowline = 1 To 50 strSQL = myquerystring (INSERT 5 columns per excel row) rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic Next rowline 基本上,

更新的问题…见下文

我有一个excel工作表,可以访问MySQL数据库作为后端

我用以下方法将新记录插入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查询),一次插入从第1行到第50行的所有50个列(每个列上有5个列)

目前它正在执行50个单独的插入查询,所以我试图将其减少到1,但我不知道这是否可行

新信息:

您好,按照您的建议和链接(谢谢!)和一些谷歌搜索,我最终得到了以下代码。。。 它工作正常,但是插入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

如果我运行一条语句并且不希望得到回复,我通常会选择VBA中的
命令
对象。

我会将open命令移到循环之外,然后使用Execute方法在循环中进行插入。您不希望每次都在数据库上执行打开操作

这是一个很好的页面

这里有一个关于(和更新)的问题


编辑:查看Remou评论中的链接,我意识到你甚至不需要打开。只需对插入使用Execute方法。

在下面找到一个有效的流程,以防其他人将来遇到同样的问题。 可能不是最好的解决方案,但在不到一秒钟的时间内一次性保存了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

这个代码对我有用

    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

可能感兴趣:谢谢你的意见。我显然走错了路,还有很多东西要学。