在 Django 中插入语言字符

发布于 2024-09-09 21:22:26 字数 2736 浏览 7 评论 0原文

我正在点击链接 http://code.google.com/p/django- multilingual-model/ 基本上我试图将印地文字符插入 mysql db

我已经在测试目录中创建了文件,如上面所述的链接并使用 django 版本 1.1 dpython 版本是 2.4.3

我收到错误消息如下。

  from testlanguages import Language,BookTranslation,Book
  >>> lang_pl = Language(code="pl", name="Polish")
  >>> lang_pl.save()
  >>> book_pl.language = lang_pl
  >>> book_pl.save()
  >>> lang_hi = Language(code="hi", name="Hindi")
  >>> lang_hi.save()
  >>> book_hi = BookTranslation()
  >>> book_hi.title = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"  Sum characters as in bbc.co.uk/hindi
  >>> book_hi.description = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच" Sum characters as in bbc.co.uk/hindi
  >>> book_hi.model = book
  >>> book_hi.language = lang_hi
  >>> book_hi.save()
  Traceback (most recent call last):
    File "<console>", line 1, in ?
    File "/opt/project/django/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
    File "/opt/project/django/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
    File "/opt/project/django/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
    File "/opt/project/django/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
    File "/opt/project/django/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
    File "/opt/project/django/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params) 
    File "/opt/project/django/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
    File "/opt/project/django/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute
    self._warning_check()
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check
    warn(w[-1], self.Warning, 3)
    File "/usr/lib/python2.4/warnings.py", line 61, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
    File "/usr/lib/python2.4/warnings.py", line 96, in warn_explicit
        raise message
        Warning: Incorrect string value: '\xE0\xA4\x95\xE0\xA4\xBE...' for column 'title' at row 1

怎么解决这个问题。

有没有简单的方法将特殊字符插入数据库

I am following the link http://code.google.com/p/django-multilingual-model/ Basically i am trying to insert hindi characters into mysql db

I have created the files in the test directory as in the above said link and using django version 1.1 an dpython version is 2.4.3

I geta error message as the following.

  from testlanguages import Language,BookTranslation,Book
  >>> lang_pl = Language(code="pl", name="Polish")
  >>> lang_pl.save()
  >>> book_pl.language = lang_pl
  >>> book_pl.save()
  >>> lang_hi = Language(code="hi", name="Hindi")
  >>> lang_hi.save()
  >>> book_hi = BookTranslation()
  >>> book_hi.title = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"  Sum characters as in bbc.co.uk/hindi
  >>> book_hi.description = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच" Sum characters as in bbc.co.uk/hindi
  >>> book_hi.model = book
  >>> book_hi.language = lang_hi
  >>> book_hi.save()
  Traceback (most recent call last):
    File "<console>", line 1, in ?
    File "/opt/project/django/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
    File "/opt/project/django/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
    File "/opt/project/django/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
    File "/opt/project/django/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
    File "/opt/project/django/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
    File "/opt/project/django/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params) 
    File "/opt/project/django/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
    File "/opt/project/django/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute
    self._warning_check()
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check
    warn(w[-1], self.Warning, 3)
    File "/usr/lib/python2.4/warnings.py", line 61, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
    File "/usr/lib/python2.4/warnings.py", line 96, in warn_explicit
        raise message
        Warning: Incorrect string value: '\xE0\xA4\x95\xE0\xA4\xBE...' for column 'title' at row 1

How to resolve this.

Is there any easy method to insert the special characters into DB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

超可爱的懒熊 2024-09-16 21:22:27

停止使用字节串。开始使用unicode

book_hi.title = u"का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"

Stop using bytestrings. Start using unicode.

book_hi.title = u"का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"
柒七 2024-09-16 21:22:27
conn = MySQLdb.connect(  host = "localhost",
            user = "root",
            passwd = "",
            db = "test");

conn.set_character_set('utf8')

这段代码将解决您的问题

注意*。conn.set_character_set('utf8')是值得注意的部分

conn = MySQLdb.connect(  host = "localhost",
            user = "root",
            passwd = "",
            db = "test");

conn.set_character_set('utf8')

this code will solve you problem

Note *. conn.set_character_set('utf8') is notable part

旧时光的容颜 2024-09-16 21:22:27

这很可能是 MySQL 的问题,默认情况下它并不总是支持 Unicode 字符。

您可能应该在数据库上将 DEFAULT CHARSET 声明为 utf8 并将 DEFAULT COLLATION 声明为 utf8_general_ci(使用 >ALTER DATABASE 或修改 CREATE DATABASE 语句,如 此处)。

This is most likely a MySQL problem, which doesn't always support Unicode characters by default.

You should probably declare the DEFAULT CHARSET to utf8 and DEFAULT COLLATION to utf8_general_ci on your database (use the ALTER DATABASE or modify the CREATE DATABASE statements, as explained here).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文