How to insert emoji into MYSQL 5.5 and higher using Django ORM

前提是你 提交于 2019-12-03 13:05:49

问题


I am trying to insert emoji's into a certain filed in my mysql table. I ran alter command and changed the collation to "utf8mb4_general_ci"

  ALTER TABLE XYZ MODIFY description VARCHAR(250) CHARACTER SET utf8mb4
  COLLATE utf8mb4_general_ci;

Table details after above query:

+-------------+--------------+---------------+--------------------+
| Column      | Type         | Character Set | Collation          |
+-------------+--------------+---------------+--------------------+
| description | varchar(250) | utf8mb4       | utf8mb4_general_ci |
+-------------+--------------+---------------+--------------------+

After this I ran the query to update description column with emoji's, every time I ran below query, the emoji is replaced by '?'.

  update XYZ set description='a test with : 😄😄' where id = 1;

But when i print the result from a select query for the same id, it displays' '?' in place of emoji. The result was:

  "a test with : ??"

Carried out necessary changes into model file. Please accept my Apologies for not making it clear, would appreciate any lead in this matter.


回答1:


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        ...
        'OPTIONS': {
                    'charset': 'utf8mb4',
                    'use_unicode': True, },
    },
}

my.cnf:

[mysqld]
character-set-server=utf8mb4
default-collation=utf8mb4_unicode_ci

[client]
default-character-set=utf8mb4


来源:https://stackoverflow.com/questions/44496101/how-to-insert-emoji-into-mysql-5-5-and-higher-using-django-orm

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