Incorrect string value: ' \xf0\x9f\x92\x90

Source: Internet
Author: User

1. Generally speaking, Mysql (less than 5.5.3) character set to UTF8, specifies that the character set of the connection is also Utf8,django in the save Unicode string is a problem with wood. However, when there are special characters in the string (such as emoji emoticons, and other characters that utf8 to occupy 4 bytes), there is a problem, and an error incorrect string value: ' \xf0\x9f\x92\x90 </. ' for column ' xxx ' at row 1

We all know that Unicode is a standard, UTF8 is a Unicode implementation, some Unicode characters to utf8 may be 4 bytes, and before MySQl5.5.3, UTF8 is only 3 bytes long.

Mysql> show Character set;
+------------+----------------------------+------------------------+----------+
| Charset | Description | Default Collation | MaxLen |
+------------+----------------------------+------------------------+----------+
| UTF8 | UTF-8 Unicode |      Utf8_general_ci | 3 |
+------------+----------------------------+------------------------+----------+

So, this Unicode character, which requires 4 bytes to be represented, is truncated and cannot be saved.


2. Low version mysql<5.5.3 seemingly no good way to change the field type to Mediumblob , nothing else to change (continue to keep the database character set and the connection character set are UTF8), problem solving. See the figure below,

Mysql> Show variables like ' char% ';
+--------------------------+----------------------------+
| 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/ 
| +--------------------------+----------------------------+
In this state MediumblobWe can handle it.


3.mysql>=5.5.3, you don't have to do it like that.

3.1 Modify the MySQL configuration file, set the default character set UTF8MB4, including collation

[Client]
Default-character-set = Utf8mb4

[MySQL]
Default-character-set = Utf8mb4

[Mysqld]
Character-set-client-handshake = FALSE
Character-set-server = Utf8mb4
Collation-server = Utf8mb4_unicode_ci
init_connect= ' SET NAMES utf8mb4 '

3.2 Reboot to confirm the above configuration is in effect

Mysql> show VARIABLES WHERE variable_name like ' character\_set\_% ' or variable_name like ' collation% ';
+-------------------------------------+------------------------------+
| variable_name | Value |
+-------------------------------------+------------------------------+
| character_set_client | UTF8MB4 |
| character_set_connection | UTF8MB4 |
| Character_set_database | UTF8MB4 |
| Character_set_filesystem | binary |
| Character_set_results | UTF8MB4 |
| Character_set_server | UTF8MB4 |
| Character_set_system | UTF8 |
| collation_connection | Utf8mb4_unicode_ci |
| Collation_database | Utf8mb4_unicode_ci |
| Collation_server | Utf8mb4_unicode_ci |
+-------------------------------------+-------------------------------+

No other changes, all with UTF8MB4, any Unicode characters in Django can be stored in MySQL.


Train of thought: Judge your MySQL UTF8 max length is not 4,

If not, support for UTF8MB4 is not supported,

If not supported, upgrade or Mediumblob

In fact, the problem, online already too much, nothing good to write, remember a pen, only for personal growth record.


The draft does not forget to dig the well person, fixes the bug to refer to these two articles.

http://vivisidea.iteye.com/blog/1395571

Http://www.linuxidc.com/Linux/2013-05/84360.htm


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.