MySQL 無法正確插入二進位制資料? 應該使用哪種資料型別?


為此,請使用 BIT 資料型別。我們首先建立一個表 -

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

使用 insert 命令在表中插入一些記錄 -

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)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將生成以下輸出。現在,您會看到記錄不可見 -

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

要顯示上述記錄(二進位制資料),您需要使用 bin() -

mysql> select bin(binaryValue) from DemoTable;

這將生成以下輸出 >

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

我們現在看另一個示例。

以下是用於插入二進位制資料的查詢,但當使用 select 時看不到值 -

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)

讓我們再次檢查表記錄,並使用 bin() 進行顯示 -

mysql> select bin(binaryValue) from DemoTable;

這將生成以下輸出 -

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

更新於:2019-08-22

533 次瀏覽

開啟您的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.