Why is a string field resulting from a CONCAT statement imported as blob when using Database Toolbox with MySQL?

2 visualizzazioni (ultimi 30 giorni)
I am importing data from a MySQL database using Database Toolbox. One of the fields of my query is the result of a concatenation.
e = exec(conn, 'SELECT CONCAT(field1," is equal to ",field2) from test')
The data is imported as an array of numbers of type int8 which appear to be ASCII values. If I use the CHAR function, the correct characters are displayed.
By examining the attributes of the field, I see that the field is of type blob.
fieldName: 'CONCAT(field1," is equal to ",field2)'
typeName: 'blob'
typeValue: -4
columnWidth: 16777215
precision: []
scale: []
currency: 'false'
readOnly: 'true'
nullable: 'true'
Message: []

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 27 Giu 2009
The reason this is happening is because MySQL is converting the data to a binary string instead of a normal text field. When importing binary data into MATLAB, there is no automatic way to detect that this is character data so MATLAB imports the data as raw integers corresponding to the binary data.
According to the MySQL documentation, if all arguments of CONCAT are non-binary strings, the result is a non-binary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:
e = exec(conn, 'SELECT CONCAT(field1," is equal to ",CAST(field2 AS CHAR)) from test');

Più risposte (0)

Tag

Non è stata ancora inserito alcun tag.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by