本文主要是介绍Django个人博客搭建教程---models插入中文报错(1366, Incorrect string value: '\\xE5...),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
models如下
class Resume(models.Model):GENDER_CHOICES = (('male', "男"),('female', "女"),)id = models.AutoField(primary_key=True)sex = models.CharField(max_length=5,choices=GENDER_CHOICES,default="male",verbose_name="性别")name = models.CharField(max_length=10)birthday = models.DateField()
当我在后台插入中文的时候报错
(1366, "Incorrect string value: '\\xE5\\x8D\\x95\\xE6\\xB2\\x99...' for column 'name' at row 1")
然后发现是数据库表没有用utf-8编码
然后去改数据库表的编码
本人用的mac,终端进入输入mysql报错:
zsh: command not found: mysql
解决办法
cd /usr/local/bin
ln -fs /usr/local/mysql/bin/mysql mysql
然后进入数据库
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/local/mysql-5.7.26-macos10.14-x86_64/share/charsets/ |
+--------------------------+-----------------------------------------------------------+
8 rows in set (0.01 sec)
发现一切正常啊,这就很过分
mysql> show full columns from JiaBlog_resume-> ;
+----------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+----------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| sex | varchar(5) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| name | varchar(10) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| birthday | date | NULL | NO | | NULL | | select,insert,update,references | |
+----------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
4 rows in set (0.00 sec)
看下这张表的编码,发现name、sex、都不是utf-8编码
改吧
mysql> alter table JiaBlog_resume modify name varchar(30) character set utf8-> ;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
然后大功告成
这篇关于Django个人博客搭建教程---models插入中文报错(1366, Incorrect string value: '\\xE5...)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!