Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 341
Shrey Avatar asked Jun 12 '17 09:06

Shrey


1 Answers

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
like image 51
Rick James Avatar answered Oct 07 '22 02:10

Rick James