VBA ADODB SQL 查询 returns "Automation error" 从单元格读取变量时,在 VBA 代码中赋值时效果很好
VBA ADODB SQL query returns "Automation error" when reading a variable from cell, works well when a value is assigned in VBA code
调试器 returns 自动化错误 运行 以下 :
Private Sub setDB()
Dim SQL As String
Dim Var As String
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Var = Worksheets(3).Range("B2").Value
SQL = "UPDATE ps_product SET ean13='" & Var & "' WHERE id_product=12"
conn.Execute (SQL)
然而,当我像这样给 Var 赋值时:Var=10
,代码运行正常。我在这里错过了什么吗?我在互联网上搜索了几天,但没有找到类似的东西。有人可以帮助或发送 link 到类似的问题吗?
当所需的单元格作为参数传递时,查询正在运行。 (感谢@Parfait 的提示)
可以在这里找到一些帮助我解决这个问题的有用信息:
VBA, ADO.Connection and query parameters
https://docs.microsoft.com/en-us/sql/ado/guide/data/creating-and-executing-a-simple-command?view=sql-server-ver15
https://docs.microsoft.com/en-us/sql/ado/guide/data/passing-parameters-to-a-named-command?view=sql-server-ver15
Working example:
Private Sub setDB()
Dim Cm As New ADODB.Command
Dim Rs As New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Test = Worksheets(3).Range("B2").Value
CommandText = "UPDATE ps_product SET ean13=? WHERE id_product=12;"
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Cm.CommandText = CommandText
Cm.CommandType = adCmdText
Cm.Name = "Var"
Set Cm.ActiveConnection = conn
conn.Var Test, Rs
End Sub
考虑参数化,这是将应用层值绑定到已执行 SQL 查询的首选方法。 ADO 通过 Command parameters 支持这种方法。如果字符串值包含单引号等特殊字符,这可以避免混乱的连接、引号标点符号和转义需求。
Dim Sql As String, Var As String
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command ' NEW OBJECT TO INITIALIZE
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
' PREPARED STATEMENT WITH PLACEHOLDER (NO QUOTES OR CONCATENATION)
Sql = "UPDATE ps_product SET ean13=? WHERE id_product=12"
' CONVERT TO NEEDED TYPE
Var = CStr(Worksheets(3).Range("B2").Value)
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = Sql
.CommandType = adCmdText
' BIND PARAMS AND DEFINE TYPE AND LENGTH
.Parameters.Append .CreateParameter("prm", adVarChar, adParamInput, 255, Var)
' EXECUTE ACTION
.Execute
End cmd
调试器 returns 自动化错误 运行 以下 :
Private Sub setDB()
Dim SQL As String
Dim Var As String
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Var = Worksheets(3).Range("B2").Value
SQL = "UPDATE ps_product SET ean13='" & Var & "' WHERE id_product=12"
conn.Execute (SQL)
然而,当我像这样给 Var 赋值时:Var=10
,代码运行正常。我在这里错过了什么吗?我在互联网上搜索了几天,但没有找到类似的东西。有人可以帮助或发送 link 到类似的问题吗?
当所需的单元格作为参数传递时,查询正在运行。 (感谢@Parfait 的提示) 可以在这里找到一些帮助我解决这个问题的有用信息:
VBA, ADO.Connection and query parameters https://docs.microsoft.com/en-us/sql/ado/guide/data/creating-and-executing-a-simple-command?view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/ado/guide/data/passing-parameters-to-a-named-command?view=sql-server-ver15
Working example:
Private Sub setDB()
Dim Cm As New ADODB.Command
Dim Rs As New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Test = Worksheets(3).Range("B2").Value
CommandText = "UPDATE ps_product SET ean13=? WHERE id_product=12;"
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
Cm.CommandText = CommandText
Cm.CommandType = adCmdText
Cm.Name = "Var"
Set Cm.ActiveConnection = conn
conn.Var Test, Rs
End Sub
考虑参数化,这是将应用层值绑定到已执行 SQL 查询的首选方法。 ADO 通过 Command parameters 支持这种方法。如果字符串值包含单引号等特殊字符,这可以避免混乱的连接、引号标点符号和转义需求。
Dim Sql As String, Var As String
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command ' NEW OBJECT TO INITIALIZE
Set conn = New ADODB.Connection
conn.Open "DRIVER={MariaDB ODBC 3.0 Driver}" _
& ";SERVER=" & "localhost" _
& ";DATABASE=" & "pbx" _
& ";USER=" & "root" _
& ";PASSWORD=" & "r00t" _
' PREPARED STATEMENT WITH PLACEHOLDER (NO QUOTES OR CONCATENATION)
Sql = "UPDATE ps_product SET ean13=? WHERE id_product=12"
' CONVERT TO NEEDED TYPE
Var = CStr(Worksheets(3).Range("B2").Value)
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = conn
.CommandText = Sql
.CommandType = adCmdText
' BIND PARAMS AND DEFINE TYPE AND LENGTH
.Parameters.Append .CreateParameter("prm", adVarChar, adParamInput, 255, Var)
' EXECUTE ACTION
.Execute
End cmd