代码之家  ›  专栏  ›  技术社区  ›  Travis Heseman

用VBA在MySQL Blob中存储字节数组

  •  1
  • Travis Heseman  · 技术社区  · 14 年前

    2 回复  |  直到 14 年前
        1
  •  3
  •   Fink    14 年前

    这里有一些代码。需要对Microsoft Active Data Objects 2.x库的引用。它使用oledb provider for MySQL(可能需要在客户机上安装)。

    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
  •  1
  •   Danny Holstein    5 年前

    mysql\u real\u escape\u string\u引号 VBA中的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
    

    它已经针对大量数据进行了优化,没有太多的条件限制( 如果 ).

        3
  •  0
  •   GSerg    14 年前