invalid byte sequence for encoding "UTF8": 0xe99d2c,utf80xe99d2c

來源:互聯網
上載者:User

invalid byte sequence for encoding "UTF8": 0xe99d2c,utf80xe99d2c


Query failed: ERROR:  invalid byte sequence for encoding "UTF8": 0xe99d2c


原因是用戶端字元集和插入內容的字元集不匹配。PostgreSQL預設不做字元集轉換,如果資料庫是UTF8的字元集,一般終端的中文字元集會設定為GBK(可以看LANG環境變數確認),所以這個編碼不經轉換的存入資料庫中,而資料庫是UTF8的,PostgreSQL發現不是UTF8編碼,就報上面的錯。
要想開啟自動字元集轉換功能,必須告訴 pg 用戶端使用的字元集。這時可以設定pg用戶端編碼為GBK,pg就會自動做字元集轉換。

下面是實驗:



1
[root@hostalonetest ~]# psql -h 192.168.18.210 -Upostgres beiigang
psql.bin (9.3.5, server 9.1.14)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.


beiigang=# 


2
beiigang=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 beiigang   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 


3
beiigang=# show server_encoding;
 server_encoding 
-----------------
 UTF8
(1 row)


4
beiigang=# show client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)


5
beiigang=# create table tb_tt (id int, ctnr varchar(60));
CREATE TABLE


6
beiigang=# insert into tb_tt(id,ctnr) values(1,'新華網');
ERROR:  invalid byte sequence for encoding "UTF8": 0xd0c2


7
beiigang=# \encoding GBK


8
beiigang=# show client_encoding;
 client_encoding 
-----------------
 GBK
(1 row)


9
beiigang=# insert into tb_tt(id,ctnr) values(1,'新華網');
INSERT 0 1


10
beiigang=# select * from tb_tt;
 id |  ctnr  
----+--------
  1 | 新華網
(1 row)


11
beiigang=# show client_encoding;
 client_encoding 
-----------------
 GBK
(1 row)


beiigang=# 
beiigang=# reset client_encoding;
RESET
beiigang=# show client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)


寫代碼時可以根據情況在建立資料庫連結時指定用戶端字元集編碼

參考:

http://www.postgresql.org/docs/9.3/interactive/multibyte.html




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

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




相關文章

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.