在 Django 中插入语言字符
我正在点击链接 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
停止使用字节串。开始使用
unicode
。Stop using bytestrings. Start using
unicode
.这段代码将解决您的问题
注意*。
conn.set_character_set('utf8')
是值得注意的部分this code will solve you problem
Note *.
conn.set_character_set('utf8')
is notable part这很可能是 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
toutf8
andDEFAULT COLLATION
toutf8_general_ci
on your database (use theALTER DATABASE
or modify theCREATE DATABASE
statements, as explained here).