【发布时间】:2011-10-17 05:26:08
【问题描述】:
我有一个列表,想通过 django raw sql 传递。
这是我的清单
region = ['US','CA','UK']
我在这里粘贴原始 sql 的一部分。
results = MMCode.objects.raw('select assigner, assignee from mm_code where date between %s and %s and country_code in %s',[fromdate,todate,region])
现在它给出了以下错误,当我在 django python shell 中执行它时
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1412, in __iter__
query = iter(self.query)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 73, in __iter__
self._execute_query()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 87, in _execute_query
self.cursor.execute(self.sql, self.params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 86, in execute
return self.cursor.execute(query, args)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
DatabaseError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1")
我也尝试过传递元组,但没有用。谁能帮帮我。
谢谢 维克拉姆
【问题讨论】:
-
你能给我们你的应用试图执行的 sql 查询字符串吗?
-
这是我的 sql 查询,它正在正确执行并且正在检索结果。
select assigner, assignee from mm_code where date between '2011-07-21' and '2011-07-31' and country_code in ('US','UK','CA')我尝试了很多组合但总是报错:-( -
您确定您的框架构建了相同的查询吗?
-
Yes.. 上面的查询在 Django Shell 中执行最新条件,但是当我给出 country_code 条件时,它给出错误。请帮帮我
-
有人能回答这个问题吗