在我的 Django 应用程序中,我上传了一个文本文件,使用 file.read() 获取文件的内容,然后保存到数据库中(使用 Django 的 .save() 方法)。
我收到以下错误:
Environment:
Request Method: POST
Request URL: http://localhost:8000/
Django Version: 1.2.5
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.markup',
'files']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/pymodules/python2.7/django/core/handlers/base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "/home/mcrittenden/Dropbox/Code/dropdo-django/dropdo/files/views.py" in index
31. return handle_upload(request.FILES['file'])
File "/home/mcrittenden/Dropbox/Code/dropdo-django/dropdo/files/views.py" in handle_upload
60. file.save()
File "/usr/lib/pymodules/python2.7/django/db/models/base.py" in save
458. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/pymodules/python2.7/django/db/models/base.py" in save_base
551. result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/pymodules/python2.7/django/db/models/manager.py" in _insert
195. return insert_query(self.model, values, **kwargs)
File "/usr/lib/pymodules/python2.7/django/db/models/query.py" in insert_query
1524. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/pymodules/python2.7/django/db/models/sql/compiler.py" in execute_sql
788. cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/pymodules/python2.7/django/db/models/sql/compiler.py" in execute_sql
732. cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.7/django/db/backends/util.py" in execute
15. return self.cursor.execute(sql, params)
File "/usr/lib/pymodules/python2.7/django/db/backends/mysql/base.py" in execute
86. return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py" in execute
168. if not self._defer_warnings: self._warning_check()
File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py" in _warning_check
82. warn(w[-1], self.Warning, 3)
Exception Type: Warning at /
Exception Value: Incorrect string value: 'xEFxBBxBF# W...' for column 'contents' at row 1
我假设(因为 EF BB BF 是 UTF BOM 字符)这是由于数据库和文件之间的字符集不同?这听起来有效吗?如果是这样,我该如何解决?
你走在正确的道路上。检查数据库的字符集(是 utf-8 吗?如果不是,并且您想要使用 UTF-8,请使用此 SQL 命令更改字符集
alter table yourTableName DEFAULT CHARACTER SET utf8;
如果您想将 UTF-8 字符串转换回来,请阅读这个关于在 Python 中使用 UTF-8 的精彩教程。
您可以使用此命令剥离 DOM。
# 从 Unicode 字符串的开头剥离 BOM(如果存在
)
) u.lstrip( Unicode( codecs.BOM_UTF8, "UTF8" )
您是对的,您正在读取的文件在其前面插入了 BOM 字符。在传递数据之前,您必须检查并去除这些字符。文件的其余部分将是 UTF-8 字符。
我不确定如何判断数据库期望的字符集。