Django:DatabaseError:接近“ ... 1”:语法错误

时间:2013-10-29 22:17:26

标签: python django sqlite utf-8 python-unicode

代码:

>>> from django.core import serializers
>>> objects = serializers.deserialize('xml', fixturestr.encode('utf8'))
>>> o = next(objects)
>>> o
<DeserializedObject: countries.Country(pk=AF)>
>>> type(o)
<class 'django.core.serializers.base.DeserializedObject'>
>>> dir(o)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'm2m_data', 'object', 'save']
>>> o.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/models/base.py", line 524, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/models/query.py", line 562, in exists
    return self.query.has_results(using=self.db)
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/models/sql/query.py", line 441, in has_results
    return bool(compiler.execute_sql(SINGLE))
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: near "����������������������������������������������������������������������������������������������������1": syntax error

查询和参数:

(Pdb) query
u'SELECT (1) AS "a" FROM "country" WHERE "country"."iso" = ?  LIMIT 1'
(Pdb) params
(u'AF',)

说实话我很难过 - 我甚至不知道该去哪儿。查询甚至不应该出现恐怖。错误消息也不会解码为utf-8。

底层任务是读入xml fixture并将其推送到数据库。不幸的是,标准loaddata命令无法处理utf-8 xml中的非ascii字符(如果您有兴趣,请参阅我最近的其他问题)。出于这个原因,我正在尝试执行loaddata所做的事情,但手动执行,以便我可以传递反序列化器utf-8编码的字节。

在linux上使用django 1.4运行python 2.7.5。

我将非常感激一种完全避免这个问题的方法,或任何关于如何解决甚至进一步诊断它的提示。

更新:这是手动尝试查询的结果:

>>> import sqlite3
>>> conn = sqlite3.connect('database.sqlite3.db')
>>> c = conn.cursor()
>>> c.execute(u'SELECT (1) AS "a" FROM "country" WHERE "country"."iso" = ?  LIMIT 1', (u'AF',))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: near "����������������������������������������������������������������������������������������������������1": syntax error
>>> c.execute(u'SELECT (1) AS "a" FROM "country" WHERE "country"."iso" = AF  LIMIT 1')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: near "����������������������������������������������������������������������������������������������������": syntax error
>>> c.execute(u'SELECT * from "country" WHERE "country"."iso" = AF  LIMIT 1')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: near "��������������������������������������������������������������������������������������������1����": syntax error
>>> c.execute(u'SELECT * from "country"')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: near "������������������������������������": syntax error
>>> c.execute('SELECT * from "country"')
<sqlite3.Cursor object at 0x4123f10>
>>> 

我的终端设置为使用utf-8。目前尚不清楚为什么传递一个unicode对象是如此可怕的错误。

更新2:这是sqlite的版本信息:

>>> sqlite3.version_info
(2, 6, 0)
>>> sqlite3.sqlite_version_info
(3, 7, 11)
>>> sqlite3.sqlite_version
'3.7.11'
>>> sqlite3.x
'11'
>>>

update 3:如果尝试使用unicode字符串,则数据库中的每个表都会发生同样的错误。

更新4:相同的错误会影响新数据库。这是运行the example code from the python docs,然后尝试执行unicode查询的结果:

>>> conn.close()
>>> conn = sqlite3.connect('example.db')
>>> c = conn.cursor()
>>> 
>>> # Create table
>>> c.execute('''CREATE TABLE stocks
...              (date text, trans text, symbol text, qty real, price real)''')
<sqlite3.Cursor object at 0x43e9180>
>>> 
>>> # Insert a row of data
>>> c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")
<sqlite3.Cursor object at 0x43e9180>
>>> 
>>> # Save (commit) the changes
>>> conn.commit()
>>> c.execute(u'SELECT * FROM "stocks"')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
OperationalError: near "����������������������������": syntax error
>>> 

更新5:提供sqlite库的包是sqlite-3.7.11-3.fc17.x86_64

1 个答案:

答案 0 :(得分:0)

这几乎可以肯定是由于我有两个不同版本的python。在我清除现有的python并从源代码重新安装后,一切正常。

对于那些想要从源代码重新编译的人:

  1. Redhat在启用UCS-4的情况下编译,而不是默认的UCS-2。您可能希望配置./configure --prefix=/usr --enable-shared --enable-unicode=ucs4
  2. 之类的内容
  3. Python有很多依赖项。这里的包管理器命令可能是找出您缺少的最简单的方法:http://docs.python.org/devguide/setup.html
  4. 请记住,如果您使用的是virtualenv,那么您的环境可能无法在您的全局python中获得更改。