Mysql
 sql >> Database >  >> RDS >> Mysql

Driver ODBC di Windows:dati emoji recuperati resi come?

Per quanto ne so, l'unico modo per recuperare caratteri supplementari Unicode (ad es. emoji) con ADODB e MySQL Connector/ODBC è recuperarli come byte — SELECT CAST(column_name AS BINARY) — e poi decodificarli. L'esempio seguente è in realtà VBA, ma modificarlo per VBScript è banale.

Option Explicit

Sub demo_retrieve_utf8mb4_value()
    Dim conn As New ADODB.Connection
    conn.Open _
        "DRIVER=MySQL ODBC 8.0 Unicode Driver;" & _
        "SERVER=localhost;PORT=3307;" & _
        "UID=root;PWD=(whatever);" & _
        "DATABASE=mydb;" & _
        "charset=utf8mb4;"
    Dim rst As New ADODB.Recordset
    rst.Open "SELECT CAST(emoji AS BINARY) FROM emoji_tbl WHERE id=1", conn
    Dim s As Variant
    s = decode_utf8(rst.Fields(0).Value)
    '
    ' do stuff with the string value

End Sub

Private Function decode_utf8(field_value As Variant) As Variant
    If IsNull(field_value) Then
        decode_utf8 = Null
    Else
        Dim strm As New ADODB.Stream
        With strm
            .Type = adTypeBinary
            .Open
            .Write field_value
            .Flush
            .Position = 0
            .Type = adTypeText
            .Charset = "UTF-8"
            decode_utf8 = .ReadText
            .Close
        End With
        Set strm = Nothing
    End If
End Function