Insert binary type data into MySQL database

32 views (last 30 days)
Marco
Marco on 9 Feb 2015
Commented: Marco on 10 Feb 2015
Dear all,
I can't figure out how to solve this issue:
I'm using matlab 2012b and my goal is to insert a value into a table's column of a MySQL database. The data type of this column is binary(2). Here you have my code:
conn=database('mysql_labview','','');
address = native2unicode(fliplr(typecast(uint16(hex2dec('0123')),'uint8')));
fastinsert(conn,'my_table',{'address'},{address});
close(conn)
I understand that native2unicode returns a string which is a char type and this is not compatible with column data type. I am able to do this operation in LabVIEW by using the byte array to string function but I need to do this operation in MATLAB too. I would really appreciate your comments and suggestions.
Thanks
Marco.
  2 Comments
Guillaume
Guillaume on 9 Feb 2015
I'm not clear on what you're trying to do. What is the data you're trying to write in your column, exactly? Here it looks like it is a single (non-unicode?) character, is that right?
If that is the case, why can't you just write the original bytes in the column?
As a side note, if I understand what you're doing, the fliplr is there to deal with endianness. Use swapbytes for that. And if you want to make your code more portable, you can even test the endianness of the machine with the third return value of computer.
Marco
Marco on 10 Feb 2015
Thanks for your side note, really useful.
The data I am trying to write in the column named address is an hexadecimal number of two bytes (in this example 0x0123). I can't insert the original vector of bytes because the data type for this column is 'binary' which is a byte string.
The only solution I see, it's to change the data type for this column, which I would really avoid.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!