MySQL isn’t inserting binary data properly? Which datatype should be used?



For this, use BIT data type. Let us first create a table −

mysql> create table DemoTable(binaryValue BIT(5)); Query OK, 0 rows affected (0.83 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(15); Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output. Now you can see the records aren’t visible −

+-------------+ | binaryValue | +-------------+ |             | |             | +-------------+ 2 rows in set (0.00 sec)

To display the above records (binary data), you need to use bin() −

mysql> select bin(binaryValue) from DemoTable;

This will produce the following output >

+------------------+ | bin(binaryValue) | +------------------+ | 1010             | | 1111             | +------------------+ 2 rows in set (0.00 sec)

Let us now see another example.

Following is the query to insert binary data, but the values aren’t visible when select is used −

mysql> insert into DemoTable VALUES (b'1010'),(b'1111'); Query OK, 2 rows affected (0.52 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select *from DemoTable; +-------------+ | binaryValue | +-------------+ |             | |             |               |             | |             | +-------------+ 4 rows in set (0.00 sec)

Let us check the table records once again and use bin() to display −

mysql> select bin(binaryValue) from DemoTable;

This will produce the following output −

+------------------+ | bin(binaryValue) | +------------------+ | 1010             | | 1111             | | 1010             | | 1111             | +------------------+ 4 rows in set (0.00 sec)
Updated on: 2019-08-22T08:48:59+05:30

646 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements