A
A
Albadur2019-04-20 11:09:40
MySQL
Albadur, 2019-04-20 11:09:40

How to add Russian characters to MySql in Django on encoding error?

In models, the field is like charfield.
English characters are entered without problems.
And the Russian is given an error.

Exception Type:	OperationalError
Exception Value:	
(1366, "Incorrect string value: '\\xD0\\x94\\xD0\\xB8\\xD0\\xBC...' for column 'title' at row 1")

The most interesting thing is that the project did not start from scratch, a foreign database was used, models were formed automatically, there are a lot of tables in the database, they read Russian characters.
The error appeared after creating and migrating a new model to the project (the rest of the models still function perfectly)
I will insert the model code:
class TbRecNews(models.Model):
    url = models.CharField(max_length=150)
    user = models.IntegerField()
    title = models.CharField(max_length=100)

Whether it is possible to solve this question 1) in models of the project? How?
2) in mysql config? How and where to look for the config itself?
Entered the command
mysqld --verbose --help grep character-set
Issued:
mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 13 - Permission denied)
2019-04-20T07:44:18.640883Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2019-04-20T07:44:18.640997Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
  --character-set-client-handshake 
                      (Defaults to on; use --skip-character-set-client-handshake to disable.)
  --character-set-filesystem=name 
  -C, --character-set-server=name 
  --character-sets-dir=name 
character-set-client-handshake                               TRUE
character-set-filesystem                                     binary
character-set-server                                         latin1
character-sets-dir                                           /usr/share/mysql/charsets/

mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.04 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)

Changed encoding:
mysql> SHOW VARIABLES LIKE 'character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> SHOW VARIABLES LIKE 'collation%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

Nothing has changed. The error remains.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-04-20
@alternativshik

Check the database connection encoding in the DATABASES settings, check the created tables and make sure that the encoding is the same everywhere and ideally utf-8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question