How to insert utf-8 mb4 character(emoji in ios5) in mysql?

我与影子孤独终老i 提交于 2019-12-16 20:56:15

问题


I am using mysql 5.5.10, and its character_sets are

| 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                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8mb4_general_ci         |
| collation_database       | utf8mb4_general_ci         |
| collation_server         | utf8mb4_general_ci         |

I changed utf8mb4 from utf8 for iOS5's emoji. They are represented by 4byte code.

But when I inserted 3 smiley emojis, '???' is in mysql.

They are 3F 3F 3F (Hex).

I can store iOS4's emojis well, but not iOS5's.

How could I store iOS5's emojis?

Please help me.


回答1:


4 byte Unicode characters aren't yet widely used, so not every application out there fully supports them. MySQL 5.5 works fine with 4 byte characters when properly configured – check if your other components can work with them as well.

Here's a few other things to check out:

  • Make sure all your tables' default character sets and text fields are converted to utf8mb4, in addition to setting the client & server character sets, e.g. ALTER TABLE mytable charset=utf8mb4, MODIFY COLUMN textfield1 VARCHAR(255) CHARACTER SET utf8mb4,MODIFY COLUMN textfield2 VARCHAR(255) CHARACTER SET utf8mb4; and so on.

    If your data is already in the utf8 character set, it should convert to utf8mb4 in place without any problems. As always, back up your data before trying!

  • Also make sure your app layer sets its database connections' character set to utf8mb4. Double-check this is actually happening – if you're running an older version of your chosen framework's mysql client library, it may not have been compiled with utf8mb4 support and it won't set the charset properly. If not, you may have to update it or compile it yourself.

  • When viewing your data through the mysql client, make sure you're on a machine that can display emoji, and run a SET NAMES utf8mb4 before running any queries.

Once every level of your application can support the new characters, you should be able to use them without any corruption.




回答2:


I’ve recently written a detailed guide on how to switch from MySQL’s utf8 to utf8mb4. If you follow the steps there, everything should work correctly. Here are direct links to each individual step in the process:

  • Step 1: Create a backup
  • Step 2: Upgrade the MySQL server
  • Step 3: Modify databases, tables, and columns
  • Step 4: Check the maximum length of columns and index keys
  • Step 5: Modify connection, client, and server character sets
  • Step 6: Repair and optimize all tables

Hope this helps.



来源:https://stackoverflow.com/questions/7814293/how-to-insert-utf-8-mb4-characteremoji-in-ios5-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!