MySQL UTF8 encoding

Source: Internet
Author: User

Do project, error

"Incorrect string value: ' \\xF0\\x9F\\x98\\x8B ' for column ' nickname ' at row 1"

The reason is that the nickname contains emoticons, the expression is 4-byte encoding, and the MySQL utf-8 encoding only supports 1-3 bytes. Starting with MYSQL5.5, support for 4 byte UTF encoding UTF8MB4,UTF8MB4 compatible UTF8, that is, UTF8 is a subset of UTF8MB4. So then the project should use the UTF8MB4 subset by default, not so much.

So the code to change the table, I have no good way. Back up the database on the line, and then manually adjust the encoding of the table that contains the nickname.

I used Django to develop it, so I looked at how Django set the default encoding. The answer is:

Hyper simple, just add to ‘OPTIONS‘: {‘charset‘: ‘utf8mb4‘} your DATABASES configuration:

DATABASES = {    ’default’: {        ’ENGINE’: ’django.db.backends.mysql’,        ’NAME’: ’example’,        ’USER’: ’example’,        ’PASSWORD’: ’example’,        ’HOST’: ’’,        ’PORT’: ’’,        ’OPTIONS’: {’charset’: ’utf8mb4’},    }}

Depending on the settings above, it is natural to use Django's migrate to take effect.

Before this project because of the number of people to handle, so did not use migrate instead of manually create the table, but also caused the settings utf8mb4 but the actual default utf-8.

Reference:

Http://blog.manbolo.com/2014/03/31/using-emojis-in-django-model-fields

https://my.oschina.net/wingyiu/blog/153357

MySQL UTF8 encoding

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.