最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Django框架报错“_mysql_exceptions.Warning: Incorrect string value:”解决方法
时间:2022-06-25 00:51:55 编辑:袖梨 来源:一聚教程网
在使用Django连到我的VPS上的MySQL数据库,在插入数据时,报了这样一个错误:“_mysql_exceptions.Warning: Incorrect string value: ‘xE6xB5x81xE8xA1x8C…’ for column ‘name’ at row 1”。
我使用了中文的数据,看起来就是一个字符集不兼容的错误;Django默认使用UTF-8,而mysqld那边配置是默认使用了latin1 – default collation 。
解决办法如下:
1. 修改 /etc/my.cnf 配置文件,然后重启mysqld。
在[client]下配置:default-character-set=utf8
在[mysqld]下配置:
default-character-set=utf8
init_connect=’SET NAMES utf8′
注意:新版MySQL(如:5.5)或MariaDB等,mysqld启动时可能会遇到“[ERROR] /usr/libexec/mysqld: unknown variable ‘default_character_set=utf8’”的错误;就应该在[mysqld]中用 character_set_server=utf8 替换掉 default_character_set=utf8 。
2.如果还没有解决,那么就得删掉原来建的DB,重新建并制定字符集为utf8,如:CREATE DATABASE `jay_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
附我遇到的报这个错的堆栈信息:
代码如下 | 复制代码 |
Traceback (most recent call last): File "/Users/jay/workspace/music-web/music/category.py", line 49, in add(data=[{'id': 1, 'name': '流行音乐'}]) File "/Users/jay/workspace/music-web/music/category.py", line 38, in add a_se.save() File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 594, in save [self.save_object(item, **kwargs) for item in self.object] File "/Library/Python/2.7/site-packages/rest_framework/serializers.py", line 1041, in save_object obj.save(**kwargs) File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 591, in save force_update=force_update, update_fields=update_fields) File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 619, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 700, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 733, in _do_insert using=using, raw=raw) File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 92, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 921, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 920, in execute_sql cursor.execute(sql, params) File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 128, in execute return self.cursor.execute(query, args) File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.9-intel.egg/MySQLdb/cursors.py", line 204, in execute if not self._defer_warnings: self._warning_check() File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.9-intel.egg/MySQLdb/cursors.py", line 117, in _warning_check warn(w[-1], self.Warning, 3) _mysql_exceptions.Warning: Incorrect string value: 'xE6xB5x81xE8xA1x8C...' for column 'name' at row 1 |
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22