MySQL 带 Excel VBA 的 ODBC 连接器 - 无法删除记录

MySQL ODBC Connector w/ Excel VBA - Can't DELETE Records

为什么可以使用 excel VBA 连接到 MySQL 数据库(通过我在下面显示的 public 函数中看到的 ODBC 连接器) INSERT 记录以及 SELECT 语句,但是当我尝试删除记录时收到错误语句?当我连接到 Root 帐户时,我仍然遇到同样的问题。我是 MySQL 的新手,正在使用安装程序时附带的 Sakila 数据库。

我可能遗漏了什么?

请参阅下面我用来连接到数据库的函数以及适用于 INSERT 和 SELECT 语句的代码示例,然后是我的 DELETE 语句出错的代码。

修订...

事实证明,这只是我的 MySQL 声明中的一个令人尴尬的错误,正如已接受的答案中所指出的那样。然而,对于那些将来偶然发现这个问题并希望通过 Excel-VBA 连接到 MySQL 的人来说,您可能会发现 connectDatabase() 和 closeDatabase() 函数的优点与大多数我在 ODBC 连接器中偶然发现的代码让用户以不太干净的格式创建他们的连接。

Public DBCONT As Object

Public Function connectDatabase()
    Set DBCONT = CreateObject("ADODB.Connection")   
    Dim Server_Name As String
    Dim Database_Name As String
    Dim User_ID As String
    Dim Password As String
    Dim sConn As String

    Server_Name = "localhost"
    Database_Name = "sakila"
    User_ID = "admin"
    Password = "password"

    sConn = "Driver={MySQL ODBC 5.1 Driver};Server=" & _
            Server_Name & ";Database=" & Database_Name & _
            ";UID=" & User_ID & ";PWD=" & Password & ";Option=3;"

    DBCONT.Open sConn
    DBCONT.cursorlocation = 3

End Function



Public Function closeDatabase()
    On Error Resume Next 
    DBCONT.Close
    Set DBCONT = Nothing
    On Error GoTo 0
End Function

以下代码有效:

Private Sub Workbook_Open()

    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")    
    Dim sqlstr As String
    sqlstr = "SELECT * FROM Actor"

    Call connectDatabase
    rs.Open sqlstr, DBCONT

    Dim actorfirstname As String
    Dim actorlastname As String
    Dim mysqlinsert As String
    actorfirstname = InputBox("Enter first name")
    actorlastname = InputBox("Enter last name")
    mysqlinsert = "INSERT INTO Actor(first_name,last_name) VALUES ('" & actorfirstname & "','" & actorlastname & "')"
    DBCONT.Execute mysqlinsert

    If rs.RecordCount > 0 Then
      For i = 1 To rs.RecordCount
        For x = 1 To rs.Fields.Count
            If i = 1 Then ThisWorkbook.Sheets("Sheet1").Cells(1, x).Value = rs.Fields(x - 1).Name
            With ThisWorkbook.Sheets("Sheet1")
                .Cells(i + 1, x).Value = rs(x - 1)
            End With
        Next x
        rs.movenext
      Next i
    Else
      MsgBox "No Records Found"
    End If
    rs.Close
    Set rs = Nothing
    Call closeDatabase

End Sub    

以下代码不起作用...它在 DBCONT.Execute sqlstrDelete 行出错,出现 运行 时间错误“-2147217900 (80040e14)”:自动化错误

Sub deleteRecord()
    Dim sqlstrDelete As String   
    sqlstrDelete = "DELETE * FROM Actor WHERE last_name='TEST'" 

    Call connectDatabase
    DBCONT.Execute sqlstrDelete
    Call closeDatabase
 End Sub

你需要星星吗? *

DELETE * FROM Actor WHERE last_name='TEST'

这会执行吗?

DELETE FROM Actor WHERE last_name='TEST'