教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 Django框架报错“_mysql_exceptions.Warning: Incorrect string value:”解决方法

Django框架报错“_mysql_exceptions.Warning: Incorrect string value:”解决方法

发布时间:2023-05-10   编辑:jiaochengji.com
教程集为您提供Django框架报错“,mysql,exceptions.Warning: Incorrect string value:”解决方法等资源,欢迎您收藏本站,我们将为您提供最新的Django框架报错“,mysql,exceptions.Warning: Incorrect string value:”解决方法资源
Django是Python Web开发框架,使用Django连接到VPS的MySQL数据库报误:“_mysql_exceptions.Warning: Incorrect string value: ‘\xE6\xB5\x81\xE8\xA1\x8C…’ for column ‘name’ at row 1”,解决方法如下

在使用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 <module>
    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

您可能感兴趣的文章:
Django是什么?一文读懂Django框架
django2.3怎么导入login包
django有什么用
要怎么学习django框架
django框架怎么实现高并发
django怎么处理高并发
Django要学到什么程度
django怎么并发
mysql提示Incorrect string value: ‘\xF0\x9F\x9B\xB3, …’ for column ‘option_value’解决办法
有关mysql不能插入中文的问题

[关闭]
~ ~