【问题标题】:Storing byte array in MySQL Blob with VBA使用 VBA 在 MySQL Blob 中存储字节数组
【发布时间】:2011-03-23 11:09:51
【问题描述】:

有人有一些 VBA 代码可以将字节数组存储到 MySQL blob 列中吗?

【问题讨论】:

标签: mysql vba bytearray blob


【解决方案1】:

这是一些代码。需要对 Microsoft Active Data Objects 2.x 库的引用。它使用 MySQL 的 OLE DB 提供程序(可能需要在客户端计算机上安装它)。

Sub StoreBLOB(data() As Byte, key As Double)
'stores the BLOB byte array into the row identified by the key
'requires reference to Microsoft Active Data Objects 2.x Library

On Error GoTo handler:

    Dim con As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim conStr As String
    Dim strSQL As String

    'have it return only the record you want to store your blob
    strSQL = strSQL & "SELECT * FROM YOURTABLE WHERE KEY = " & key

    'setup connection
    conStr = conStr & "Provider=MySQLProv;"
    conStr = conStr & "Data Source=mydb;"
    conStr = conStr & "User Id=myUsername;"
    conStr = conStr & "Password=myPassword;"

    con.ConnectionString = conStr
    con.Open

    rs.Open strSQL, con, adOpenDynamic, adLockOptimistic

    If rs.RecordCount > 1 Then
        Err.Raise 1001, "StoreBLOB", "Too many records returned from dataset.  Check to make sure you have the right key value"
    Else
        Err.Raise 1002, "StoreBLOB", "No Records found that match the key"
    End If

    rs.Fields("BLOBFIELDNAME").Value = data
    rs.Update 'store the contents to the database

    rs.Close
    con.Close
    Set rs = Nothing
    Set con = Nothing

Exit Sub
handler:
    Err.Raise 1003, "StoreBLOB", "Unexpected Error in StoreBLOB.  Check that server is running"
End Sub

【讨论】:

    【解决方案2】:

    假设你使用ADO访问mysql,有KB article on the subject

    【讨论】:

      【解决方案3】:

      我有一些代码,我在 VBA 中复制了 mysql_real_escape_string_quote C 函数,以便可以转义必要的字符并像处理常规文本一样构建 SQL:

      Function mysql_real_escape_string_quote(toStr() As Byte, fromStr() As Byte, length As Long, quote As String) As Long
          mysql_real_escape_string_quote = 0
          Dim CharMap() As Byte: CharMap = StrConv(String(256, 0), vbFromUnicode)
          CharMap(0) = Asc("0"): CharMap(39) = Asc("'"): CharMap(34) = Asc(""""): CharMap(8) = Asc("b"): CharMap(10) = Asc("n"): CharMap(13) = Asc("r"):
          CharMap(9) = Asc("t"): CharMap(26) = Asc("z"): CharMap(92) = Asc("\"): CharMap(37) = Asc("%"): CharMap(95) = Asc("_"):
      
          Dim i As Long: Dim n As Long: n = 0
          If length > UBound(fromStr) + 1 Then Exit Function
          For i = 0 To length - 1  '---count escapable chars before redim---
              n = n + 1
              If CharMap(fromStr(i)) <> 0 Then n = n + 1
          Next i
      
          ReDim toStr(n - 1) As Byte
          n = 0
          For i = 0 To length - 1  '---test chars---
              If CharMap(fromStr(i)) = 0 Then
                  toStr(n) = fromStr(i)
              Else                        '---escape char---
                  toStr(n) = Asc(quote): n = n + 1
                  toStr(n) = CharMap(fromStr(i))
              End If
              n = n + 1
          Next i
          mysql_real_escape_string_quote = n
      End Function
      
      Function mysql_real_escape_string(InputString As String) As String
          mysql_real_escape_string = ""
          Dim toStr() As Byte: Dim fromStr() As Byte
          fromStr = StrToChar(InputString)
          If mysql_real_escape_string_quote(toStr, fromStr, UBound(fromStr) + 1, "\") = 0 Then Exit Function
          mysql_real_escape_string = StrConv(toStr(), vbUnicode)
      End Function
      
      Function StrToChar(str As String) As Byte()
          Dim ans() As Byte
          ans = StrConv(str, vbFromUnicode)
          ReDim Preserve ans(Len(str)) As Byte
          ans(Len(str)) = 0
          StrToChar = ans
      End Function
      
      Sub testit()
          Dim toStr() As Byte: Dim fromStr() As Byte
          fromStr = StrToChar("hello world's")
          MsgBox (mysql_real_escape_string_quote(toStr, fromStr, UBound(fromStr) + 1, "\"))
          MsgBox (mysql_real_escape_string("hello world's"))
          For i = 0 To UBound(toStr)
              Debug.Print i & " " & toStr(i)
          Next i
      End Sub
      

      它针对大量数据进行了优化,没有大量的条件(ifs)。

      【讨论】:

        猜你喜欢
        • 2013-02-08
        • 2015-12-10
        • 2012-07-03
        • 2011-03-07
        • 2019-04-04
        • 1970-01-01
        • 2012-02-02
        • 2013-02-13
        • 2021-07-02
        相关资源
        最近更新 更多