MySQL字元編碼問題,Incorrect string value,mysqlincorrect

來源:互聯網
上載者:User

MySQL字元編碼問題,Incorrect string value,mysqlincorrect
MySQL上插入漢字時報錯如下,具體見後面分析。
Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1


MySQL字元集相關參數:
character_set_server :  伺服器字元集
collation_server     : 伺服器校對規則 


character_set_database : 預設資料庫的字元集
collation_database     : 預設資料庫的校對規則


character_set_client:伺服器使用該變數取得連結中用戶端的字元集


character_set_connection:伺服器將用戶端的query從character_set_client轉換到該變數指定的字元集。
character_set_results:伺服器發送結果集或返回錯誤資訊到用戶端之前應該轉換為該變數指定的字元集




有兩個語句可以設定串連字元集,如下:
A
SET NAMES 'charset_name' 相當於下面三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET character_set_connection = x;   #這個也設定了collation_connection的預設值x


B
SET CHARACTER SET charset_name 相當於下面三句:
mysql> SET character_set_client = x;
mysql> SET character_set_results = x;
mysql> SET collation_connection = @@collation_database;


character_set_results為NULL時,伺服器對返回結果集不做任何轉換
mysql> SET character_set_results = NULL;


因為字元集編碼引起的問題在pg上報的錯是:invalid byte sequence for encoding "UTF8",具體見參考

http://blog.csdn.net/beiigang/article/details/39582583



MySQL上字元集編碼引起的問題報:Incorrect string value,具體見下面實驗:

1
mysql> show variables like '%character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | gbk                        |
| character_set_connection | gbk                        |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | gbk                        |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


2
mysql> create table tb_tt (id int, ctnr varchar(60));
Query OK, 0 rows affected (0.06 sec)


3
mysql> show create table tb_tt;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| Table | Create Table
                                                     |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
| tb_tt | CREATE TABLE `tb_tt` (
  `id` int(11) DEFAULT NULL,
  `ctnr` varchar(60) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------+
1 row in set (0.00 sec)


4
mysql>  insert into tb_tt(id,ctnr) values(1,'新華網');
Query OK, 1 row affected (0.02 sec)


5
mysql> select * from tb_tt;
+------+--------+
| id   | ctnr   |
+------+--------+
|    1 | 新華網 |
+------+--------+
1 row in set (0.02 sec)


6
mysql> set names 'UTF8';
Query OK, 0 rows affected (0.00 sec)


7
mysql> show variables like '%character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


8
mysql>  insert into tb_tt(id,ctnr) values(2,'新華網');
ERROR 1366 (HY000): Incorrect string value: '\xD0\xC2\xC8A\xBEW' for column 'ctnr' at row 1




參考:

http://dev.mysql.com/doc/refman/5.5/en/globalization.html



----------------- 

轉載請著明出處:
blog.csdn.net/beiigang

mysql插入資料編碼問題,Incorrect string value: '\xA9O\xB5\xC4\xC1\xC1

辦法就是轉義, 找一個不會用到的特殊字元或者字串標示這個字元。
但是需要你在展現層(web或者用戶端)與持久層(資料庫)之間傳遞資料過程中,進行轉碼和解碼。
 
mysql中匯入資料出現 incorrect string value問題

使用 show variables like 'character%' 查看資料庫的編碼;
查看檔案C:/data/201109/all.txt的編碼,
以上兩者要一樣。
可以使用 alter table 表名 convert to character set 編碼格式; 事先最好備份資料,以免意外。
檔案直接 另存新檔,選擇編碼。盡量都要UTF-8
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.