Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Differentiate TEXT and BLOB fields in MySQL C interface

I’m using mysql_fetch_field_direct to get information about the type of a given column in a dynamically fetched result set like so:

MYSQL_FIELD *field = mysql_fetch_field_direct(resultSet, col);

then I access field->type to check the field’s type. However, I am currently stuck because I would like to take different actions depending on whether the field’s type is BLOB or TEXT. However, it seems that the enum_field_types enum only contains MYSQL_TYPE_BLOB, but not MYSQL_TYPE_TEXT and instead marks text as BLOBs as well. This kinda makes sense given that a TEXT object is essentially a BLOB but with a special interpretation when processing the data.

However, this leaves me wondering: How can I check whether the field’s actual type is BLOB or TEXT?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Check the field->charsetnr.

If this value is 63, it’s a binary BLOB.

If it’s any other value, it’s TEXT.

Here’s what the documentation (in the section describing charsetnr) has to say about this:

To distinguish between binary and nonbinary data for string data types, check whether the charsetnr value is 63. If so, the character set is binary, which indicates binary rather than nonbinary data. This enables you to distinguish BINARY from CHAR, VARBINARY from VARCHAR, and the BLOB types from the TEXT types.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading