2014-02-17 35 views
1

根據mysql documentation,它只支持最多3字節的utf-8 unicode編碼。 我的問題是,我怎樣才能替換我的數據庫中需要4字節utf-8編碼的字符?我如何解碼這些字符才能準確顯示用戶寫的內容?集成測試的MySQL編碼3字節的4字節utf-8 - 錯誤的字符串值

部分:

description = u'baaam á ✓ ✌ ❤' 
print description 
test_convention = Blog.objects.create(title="test title", 
              description=description, 
              login=self.user, 
              tag=self.tag) 

錯誤:

Creating test database for alias 'default'... 
baaam á ✓ ✌ ❤ 
E.. 
====================================================================== 
ERROR: test_post_blog (blogs.tests.PostTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/admin/Developer/project/pro/blogs/tests.py", line 64, in test_post_blog 
    tag=self.tag) 
    File "build/bdist.macosx-10.9-intel/egg/MySQLdb/cursors.py", line 201, in execute 
    self.errorhandler(self, exc, value) 
    File "build/bdist.macosx-10.9-intel/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
DatabaseError: (1366, "Incorrect string value: '\\xE2\\x9C\\x93 \\xE2\\x9C...' for column 'description' at row 1") 

---------------------------------------------------------------------- 
Ran 3 tests in 1.383s 

FAILED (errors=1) 
Destroying test database for alias 'default'... 

表的配置:

+----------------------------------+--------+---------+-------------------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------+----------------+---------+ 
| Name        | Engine | Version | Collation   | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time   | Update_time | Check_time | Checksum | Create_options | Comment | 
+----------------------------------+--------+---------+-------------------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------+----------------+---------+ 
| blogs_blog      | InnoDB |  10 | utf8_general_ci | Compact | 25 |   1966 |  49152 |    0 |  32768 |   0 |    35 | 2014-02-09 00:57:59 | NULL  | NULL  |  NULL |    |   | 
+----------------------------------+--------+---------+-------------------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+----------+----------------+---------+ 

更新:我已經改變從UTF表和列的配置-8到utf8mb4,仍然收到相同的錯誤, 有任何想法嗎?

+----------------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+ 
| Name        | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time   | Update_time | Check_time | Collation   | Checksum | Create_options | Comment | 
+----------------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+ 
| blogs_blog      | InnoDB |  10 | Compact | 5 |   3276 |  16384 |    0 |  32768 |   0 |    36 | 2014-02-17 22:24:18 | NULL  | NULL  | utf8mb4_general_ci |  NULL |    |   | 
+----------------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+ 

和:

+---------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ 
| Field   | Type   | Collation   | Null | Key | Default | Extra   | Privileges      | Comment | 
+---------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ 
| id   | int(11)  | NULL    | NO | PRI | NULL | auto_increment | select,insert,update,references |   | 
| title   | varchar(500) | latin1_swedish_ci | NO |  | NULL |    | select,insert,update,references |   | 
| description | longtext  | utf8mb4_general_ci | YES |  | NULL |    | select,insert,update,references |   | 
| creation_date | datetime  | NULL    | NO |  | NULL |    | select,insert,update,references |   | 
| login_id  | int(11)  | NULL    | NO | MUL | NULL |    | select,insert,update,references |   | 
| tag_id  | int(11)  | NULL    | NO | MUL | NULL |    | select,insert,update,references |   | 
+---------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+ 
+0

爲什麼不會告發」你使用4字節的UTF?這是utf8mb4,afair – akuzminsky

+0

好吧,我已經將排序規則更改爲utf8mb4,但仍然無法正常工作,但是是一個好開始,謝謝! – Pompeyo

回答

0

據支持,但不是utf8。添加以下的my.cnf[mysqld]部分:

character-set-server=utf8mb4 
collation-server=utf8mb4_unicode_ci 

在創建數據庫時,使用:

CREATE DATABASE xxxxx DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci; 

CREATE TABLE命令的末尾添加:

ENGINE=InnoDB ROW_FORMAT=COMPRESSED DEFAULT CHARSET=utf8mb4; 
相關問題