0

When in django admin panel i try to add something in database using russian symbols - it gives me error:

(1366, "Incorrect string value: '\\xD0\\xB2\\xD1\\x84\\xD1\\x8B' for column 'name' at row 1")
Request Method: POST
Request URL:    https://giver.md/dev/admin/gift/categoryru/add/
Django Version: 1.10.4
Exception Type: OperationalError
Exception Value:    
(1366, "Incorrect string value: '\\xD0\\xB2\\xD1\\x84\\xD1\\x8B' for column 'name' at row 1")
Exception Location: /home/ubuntu/giver/server/local/lib/python2.7/site-packages/MySQLdb/connections.py in defaulterrorhandler, line 36
Python Executable:  /home/ubuntu/giver/server/bin/python2
Python Version: 2.7.12

I know, that i need to change collation to resolve this problem? But how can i collate al tables in my mysql database using ubuntu 16? I tryed this, but this didn't helped me:

SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE utf8_general_ci") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="giver"

1 Answers1

2

As you mentioned, you have to change collation. The easiest way is to drop your old database and create a new utf8 one. Just note that with dropping your database, all of your tables and data will be gone, so you have to create tables and superuser again. So:

  1. Create a new utf8 database:

    CREATE DATABASE <new_database_name> CHARACTER SET utf8;
    
  2. If the name of new database differ from the old one, don't forget to change database name in your app setting.py file!

  3. Create tables; Just simply migrate:

    $ python manage.py migrate
    
  4. Create super user:

    $ python manage.py createsuperuser
    

If you have some important data in your database and you don't want to lose them, see How to convert an entire MySQL database characterset and collation to UTF-8?

or simply use proper tools to backup your data from old database and import them to the new utf8 created database.

Hamidreza
  • 1,465
  • 12
  • 31