When in django admin panel i try to add something in database using russian symbols - it gives me error:
在django管理面板中我尝试使用俄语符号在数据库中添加内容 - 它给了我错误:
(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:
我知道,我需要更改排序规则才能解决这个问题?但是如何使用ubuntu 16整理我的mysql数据库中的表?我试过这个,但这对我没有帮助:
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," COLLATE utf8_general_ci") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="giver"
1 个解决方案
#1
1
As you mentioned, you have to change collation. The easiest way: You can simply 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:
如您所述,您必须更改排序规则。最简单的方法:您可以简单地删除旧数据库并创建一个新的utf8数据库。请注意,删除数据库后,所有表和数据都将消失,因此您必须再次创建表和超级用户。所以:
-
Create a new utf8 database:
创建一个新的utf8数据库:
CREATE DATABASE <new_database_name> CHARACTER SET utf8;
-
If the name of new database differ from the old one, don't forget to change database name in your app setting.py file!
如果新数据库的名称与旧数据库的名称不同,请不要忘记在应用程序setting.py文件中更改数据库名称!
-
Create tables; Just simply migrate:
创建表格;只需简单迁移:
$ python manage.py migrate
-
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?
如果您的数据库中有一些重要数据并且您不想丢失它们,请参阅如何将整个MySQL数据库字符集和排序规则转换为UTF-8?
or simply use proper tools to backup your data from old database and import them to the new utf8 created database.
或者只是使用适当的工具从旧数据库备份数据并将它们导入新的utf8创建的数据库。
#1
1
As you mentioned, you have to change collation. The easiest way: You can simply 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:
如您所述,您必须更改排序规则。最简单的方法:您可以简单地删除旧数据库并创建一个新的utf8数据库。请注意,删除数据库后,所有表和数据都将消失,因此您必须再次创建表和超级用户。所以:
-
Create a new utf8 database:
创建一个新的utf8数据库:
CREATE DATABASE <new_database_name> CHARACTER SET utf8;
-
If the name of new database differ from the old one, don't forget to change database name in your app setting.py file!
如果新数据库的名称与旧数据库的名称不同,请不要忘记在应用程序setting.py文件中更改数据库名称!
-
Create tables; Just simply migrate:
创建表格;只需简单迁移:
$ python manage.py migrate
-
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?
如果您的数据库中有一些重要数据并且您不想丢失它们,请参阅如何将整个MySQL数据库字符集和排序规则转换为UTF-8?
or simply use proper tools to backup your data from old database and import them to the new utf8 created database.
或者只是使用适当的工具从旧数据库备份数据并将它们导入新的utf8创建的数据库。