gpt4 book ai didi

mySQL、Excel、VBA 和 ADODB(命令)参数问题

转载 作者:行者123 更新时间:2023-12-04 20:23:33 25 4
gpt4 key购买 nike

我有一个包含用户数据的数据库(本地主机)。我使用 mySQL。
我想使用 Excelsheet 更新这些数据。我写了一些 VBA 代码。我调查了this solution: adodb trouble with parameters and vba但这对我不起作用。
我做了一个 VBA 宏并做了一些测试。该宏仅适用于“普通”SQL 字符串。
我在代码中加入了不同的测试和错误。我无法弄清楚我做错了什么。
我已经用谷歌搜索了不同的错误,但我找不到足够的信息
[MySQL][ODBC 8.0(w) 驱动程序][mysqld-5.5.5-10.4.14-MariaDB] 参数类型无效
我检查了数据库和列(varChar)的值类型设置,所以看起来(?)没问题。
有人可以帮我吗?
此致
M。

Sub Addres_to_Database_2_Update()
'Update an MySQL database (database: samueldb, table: users) on localhost
'with data form an excel sheet called "Testblad"

Dim Connect As ADODB.Connection
Dim sqlQuery As String

Dim user_id As String
Dim firstname As String
Dim lastname As String
Dim address As String
Dim contact As String

user_id = Sheets("Testblad").Range("B6").Value
firstname = Sheets("Testblad").Range("B7").Value
lastname = Sheets("Testblad").Range("B8").Value
address = Sheets("Testblad").Range("B9").Value
contact = CStr(Sheets("Testblad").Range("B10").Value)

MsgBox "Data: " & user_id & " " & firstname & " " & lastname & " " & address & " " & contact

Set Connect = New ADODB.Connection
Connect.Open "DRIVER= {MySQL ODBC 8.0 Unicode Driver}; SERVER=localhost;DATABASE=samueldb;USER=root; PASSWORD=;"
'password is empty

'This normal SQL string works fine
'sqlQuery = "UPDATE users " & _
' "SET FirstName = '" & firstname & "', " & _
' "LastName = '" & lastname & "', " & _
' "address = '" & address & "', " & _
' "contact = '" & contact & "' WHERE user_id= " & user_id & " "

Debug.Print sqlQuery
'Result: UPDATE users SET FirstName = 'Henkie', LastName = 'Strijbos', address = 'Amsterdam', contact = '35673567' WHERE user_id= 23
'Result = OK
'Database will be Updated when SQL is executed

'=== But: testing with parameters does not work
'see: https://stackoverflow.com/questions/43679797

sqlQuery = "UPDATE users " & _
"SET firstname = ?firstname, " & _
"lastname = ?lastname, " & _
"address = ?address, " & _
"contact = ?contact " & _
"WHERE user_id = user_id"
Debug.Print sqlQuery
'Result when debugged > UPDATE users SET firstname = ?firstname, lastname = ?lastname, address = ?address, contact = ?contact WHERE user_id = user_id
'Result = OK


With New ADODB.command
.ActiveConnection = Connect
.CommandType = adCmdText
.NamedParameters = True
.CommandText = sqlQuery

' ========== Test 1 > Run-time error '3708':
'
' .Parameters.Append .CreateParameter("?firstname", , adParamInput, , firstname)
'RESULT: Run-time error '3708':Parameter Object is improperly defined. Inconsistent or incomplete information was provided
' code stops her
' .Parameters.Append .CreateParameter("?lastnamer", , adParamInput, , lastname)
' .Parameters.Append .CreateParameter("?address", , adParamInput, , address)
' .Parameters.Append .CreateParameter("?contact", , adParamInput, , contact)

'========== Test 2 This does not work, same failure > Run-time error '3708':
'
' .Parameters.Append .CreateParameter("?firstname", , , , firstname)
'RESULT: Run-time error '3708':Parameter Object is improperly defined. Inconsistent or incomplete information was provided
' code stops her
' .Parameters.Append .CreateParameter("?lastnamer", , , , lastname)
' .Parameters.Append .CreateParameter("?address", , , , address)
' .Parameters.Append .CreateParameter("?contact", , , , contact)

'========== Test 3 This does not work, Run-Time error 13
'
' .Parameters.Append .CreateParameter("?firstname", firstname)
'Result: Run-Time error 13: Type Mismatch.
' code stops her
' .Parameters.Append .CreateParameter("?lastnamer", lastname)
' .Parameters.Append .CreateParameter("?address", address)
' .Parameters.Append .CreateParameter("?contact", contact)

'======= Test 4 This does not work
'
' .Parameters.Append .CreateParameter("?firstname", adChar, adParamInput, 30, firstname)
' .Parameters.Append .CreateParameter("?lastnamer", adChar, adParamInput, 30, lastname)
' .Parameters.Append .CreateParameter("?address", adChar, adParamInput, 150, address)
' .Parameters.Append .CreateParameter("?contact", adChar, adParamInput, 20, contact)

'Debug.Print sqlQuery
'Result when debugged > UPDATE users SET firstname = ?firstname, lastname = ?lastname, address = ?address, contact = ?contact WHERE user_id = user_id
'I do not understand, because the values should be appointed

'should it not be:
'UPDATE users SET FirstName = 'Henkie', LastName = 'Strijbos', address = 'Amsterdam', contact = '35673567' WHERE user_id= 23

'When executed >Run-time Error -2147217887 (80040e21)
' [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.4.14-MariaDB] Invalid parameter Type

'=========== Test 5 This does not work
.Parameters.Append .CreateParameter("?", adChar, adParamInput, 30, firstname)
.Parameters.Append .CreateParameter("?", adChar, adParamInput, 30, lastname)
.Parameters.Append .CreateParameter("?", adChar, adParamInput, 150, address)
.Parameters.Append .CreateParameter("?", adChar, adParamInput, 20, contact)

Debug.Print sqlQuery
'Result when debugged > UPDATE users SET firstname = ?firstname, lastname = ?lastname, address = ?address, contact = ?contact WHERE user_id = user_id
'I do not understand, because the values should be appointed

'should it not be:
'UPDATE users SET FirstName = 'Henkie', LastName = 'Strijbos', address = 'Amsterdam', contact = '35673567' WHERE user_id= 23

'When executed >Run-time Error -2147217887 (80040e21)
' [MySQL][ODBC 8.0(w) Driver][mysqld-5.5.5-10.4.14-MariaDB] Invalid parameter Type
' ==
.Execute
End With

Connect.Close
End Sub

最佳答案

我从来没有设法使用命名参数,所以我只会展示如何使用未命名参数。
a) sql 命令文本得到一个简单的?在要插入参数的地方。

sqlQuery = "UPDATE users " _
& " SET firstname = ?, lastname = ?, address = ?, contact = ? " _
& " WHERE user_id = ?"
b) 参数必须以正确的顺序附加。您可以设置参数名称,但它是可选的。重要的是顺序,您需要为每个 ? 创建一个参数。在您的查询中。您可能还有 Where 的参数- 子句(在您当前的查询中,您将更新整个表,因为 where 子句 user_id = user_id 对所有记录都为真(除了那些 user_id 为空的记录)。
 .Parameters.Append .CreateParameter(, adChar, adParamInput, 30, firstname)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 30, lastname)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 150, address)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 20, contact)
.Parameters.Append .CreateParameter(, adChar, adParamInput, 20, user_id)
c) 不是 100% 确定 mySQL,但我认为您需要将参数类型设置为 adVarChar,将长度参数设置为实际字符串长度。
 .Parameters.Append .CreateParameter(, adVarChar, adParamInput, len(firstName), firstname)
(...)
d) 与 Debug.Print ,您将始终看到您输入的命令文本。该命令与参数一起发送到数据库引擎,引擎会解决这个问题。 ADODB 不关心在数据库端使用的语法。这可以让您和 ADODB 从为 Date 参数添加所有正确引号或正确语法或使用哪个字符作为小数分隔符的噩梦中解脱出来。

关于mySQL、Excel、VBA 和 ADODB(命令)参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66532094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com