The article is for study only, if there are mistakes, welcome to point out
Today I encountered some errors while working on a Local Windows Django project connecting to a Mysql database on a Linux cloud server
Question 1: After connecting to the database, when we successfully created a table, we were too busy to delete the table in the database. And then we use
python manage.py makemigrations
python manage.py migrate
Copy the code
When we went to regenerate the table, we found that we couldn’t regenerate the table,
(django_xadmin_pb-y4pnuwd9) C:\Users\Alpaca\Desktop\ PyWeb project \ django_xadmin_pb-one_project > Python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, polls, sessions Running migrations: No migrations to apply.Copy the code
1. Delete all the tables in the database (because there is association between the database tables, so we need to delete several times) 2. Re-use these two commands in a Django project, and they are generated successfully
Mysql database cannot create insert Chinese content example when creating table
File "C:\Users\Alpaca\.virtualenvs\django_xadmin_pb-y4pNUwd9\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
db.query(q)
File "C:\Users\Alpaca\.virtualenvs\django_xadmin_pb-y4pNUwd9\lib\site-packages\MySQLdb\connections.py", line 277, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xE5\\x93\\x88\\xE5\\x93\\x88' for column 'name' at row 1")
Copy the code
I deleted part of the error, mainly look at the last sentence, he reported the error reason is that my hahaha code can not be created in name after checking the post, it is said that mysql after installing latin1 encoding, but Chinese we need to use UTF8 encoding, so we need to solve the next several steps
# Check the error cause
mysql> show variables like 'character_set%'; Or mysql > status;Copy the code
Explain | character_set_client # client character set | | utf8 character_set_connection character set | | utf8 # link character_set_database | utf8 # database character set, the configuration file specifies or create specified when | character_set_results | utf8 # returns the character set | character_set_server | utf8 # server character set, configuration file, or create a library, table when specified
1. Go to /etc/mysql.my. CNF and add
[client]
default-character-set =utf8
[mysql]
default-character-set =utf8
[mysqld]
collation-server = utf8_unicode_ci
init_connect ='SET NAMES utf8'character_set_server=utf8 ! includedir /etc/mysql/conf.d/ ! includedir /etc/mysql/mysql.conf.d/Copy the code
As you can see, my.cnf should be a personal configuration that inherits from the default configuration. In this case, we just need to modify the content here
2. Restart the mysql
service mysql restart
Copy the code
Check whether the command succeeds
mysql> status;
Copy the code
It’s already in UTF-8 form
# note! There is a pitfall here, although we have configured it successfully, but this configuration will only apply to the database tables you create later, and will not apply to the database tables before this configuration.