Friday, October 2, 2009

Inserting binary values in the mysql shell

I needed to insert some dummy values into a table which had a bit field as well as a blob field, both of which are "NOT NULL". You just need to have the 'b' prefix on the data, like so:


INSERT INTO faketable(blob1, name, bitfield) values(b'001011101', "Beeblebrox", b'1');


You can also print out the binary data in a way that's not going to ruin your terminal using the BIN(), OCT() and HEX() functions:


SELECT name, HEX(blob1), BIN(bitfield) from faketable;
+--------------------+--------------+----------------+
| name | HEX(blob1) | BIN(bitfield) |
+--------------------+--------------+----------------+
| Beeblebrox | 005D | 1 |
+--------------------+--------------+----------------+

No comments:

Post a Comment