【问题标题】:"Incorrect string value" error in MySql when saving Unicode保存 Unicode 时 MySql 中出现“不正确的字符串值”错误
【发布时间】:2013-08-26 19:09:23
【问题描述】:

当我尝试将类似 украден 的内容添加到组名字段时出现此错误:

(1366, "Incorrect string value: '\\xD1\\x83\\xD0\\xBA\\xD1\\x80...' for column 'group_name' at row 1")

起初,如果发生该错误,则不会创建任何组。但是,在我运行 MySql 命令后,在我的 django 数据库上将发生错误的特定列的编码从 utf8 更改为 utf8_general_ci 后,该组是使用我输入的 unicode 字符创建的,但仍然出现上述错误当我使用 unicode 字符修改组名时显示。

模型中的 Unicode 方法:

def __unicode__(self):
    return u'{}/{} (Group {})'.format(self.id, self.name,
                                      self.owner_group_id)

【问题讨论】:

  • 您是否为模型定义了__unicode__ 方法?你确定它返回 Unicode 吗?

标签: python mysql django unicode utf-8


【解决方案1】:

我认为这对您有用,请尝试让我知道它是否有效:

import MySQLdb as mdb
con = mdb.connect('host', 'user', 'pass', 'dbname')
cur = con.cursor()
con.set_character_set('utf8')
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')

【讨论】:

  • 不,这不起作用。我认为你的语法有问题。
  • 再试一次,我掉线了import MySQLdb as mdb
  • 不,我在我的 SQL 数据库上运行了它,但我仍然遇到同样的错误。不过感谢您的建议。
猜你喜欢
  • 2017-06-14
  • 2012-02-01
  • 1970-01-01
  • 2011-04-16
  • 2017-10-25
  • 2012-11-08
  • 2017-03-18
  • 2016-09-08
  • 1970-01-01
相关资源
最近更新 更多