Analysis on how to enable Mysql to support Emoji expressions

Source: Internet
Author: User


Statement: My articles are all written after I have encountered and solved the problem in my actual work. You may have been clear for a long time or have no help for you. If you like them, please stick to them first, do not spray either. Just like football, if you don't like Chinese football, don't worry about it. You can watch table tennis, badminton, or even curling! But don't scold them. After all, there are still many people who love Chinese football. I am one of them. I recently worked on an iPhone project, I was in charge of backend, Java Development, DB is Mysql, version 5.5.21, encoding is UTF-8. Www.2cto.com but found a problem. The iPhone had an Emoji expression and failed to insert Mysql. The following exception was reported:
Java code. SQL. SQLException: Incorrect string value: '\ xF0 \ x9F \ x92 \ x94' for column 'name' at row 1 at com. mysql. jdbc. SQLError. createSQLException (SQLError. java: 1073) at com. mysql. jdbc. mysqlIO. checkErrorPacket (MysqlIO. java: 3593) at com. mysql. jdbc. mysqlIO. checkErrorPacket (MysqlIO. java: 3525) at com. mysql. jdbc. mysqlIO. sendCommand (MysqlIO. java: 1986) at com. mysql. jdbc. mysqlIO. sqlQueryDirect (MysqlIO. java: 2140) at com.mysql.jdbc.ConnectionImpl.exe cSQL (ConnectionImpl. java: 2620) at com.mysql.jdbc.StatementImpl.exe cuteUpdate (StatementImpl. java: 1662) at com.mysql.jdbc.StatementImpl.exe cuteUpdate (StatementImpl. java: 1581) Google hundreds of times and finally found the answer. The UTF-8 encoding may be two, three, or four bytes. The Emoji expression contains four bytes, while Mysql utf8 encoding can contain up to three bytes. Therefore, data cannot be inserted. Solution: Convert Mysql encoding from utf8 to utf8mb4. I should be able to find a lot of methods to modify Mysql encoding on the Internet. I did this. Some modifications may not be necessary, but I finally solved the problem:
1. modify my. cnf [mysqld] character-set-server = utf8mb4 [mysql] www.2cto.com default-character-set = utf8mb4 modify and then restart Mysql 2. log on to Mysql as root, modify the environment variables, and change character_set_client, character_set_connection, character_set_database, character_set_results, and character_set_server to utf8mb4 3. convert the created table to utf8mb4 command: alter table TABLE_NAME convert to character set utf8mb4 collate utf8mb4_bin; (replace TABLE_NAME with your table name) Now, OK! Author RamosLi

Related Article

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.