Django MySql 原始查询错误 - 参数索引超出范围

发布于 2024-08-21 09:39:05 字数 1092 浏览 4 评论 0原文

此视图在 Windows 上的普通 pyton/Django/mysql 上运行良好
我将其移植到 jython/Django/mysql 上运行,它给出了错误 -

Exception received is : error setting index [10] [SQLCode: 0]    
Parameter index out of range (10 > number of parameters, which is 0). [SQLCode: 0],  
[SQLState: S1009]

查询是 -

cursor.execute("select value from table_name    
where value_till_dt >= str_to_date('%s,%s,%s,%s,%s', '%%m,%%d,%%Y,%%H,%%i')    
AND value_till_dt <=  str_to_date('%s,%s,%s,%s,%s', '%%m,%%d,%%Y,%%H,%%i')    
and granularity='5'    
ORDER BY value_till_dt",    
[int(tempStart.month),int(tempStart.day), int(tempStart.year), int(tempStart.hour), int(tempStart.minute),    
int(tempEnd.month), int(tempEnd.day), int(tempEnd.year), int(tempEnd.hour), int(tempEnd.minute)])

正如您所看到的,有 10 个参数传递给此查询。 该错误是否意味着查询未获取参数?

我在执行之前打印出了参数,它们显示为已正确传递 -

1 - Start Parameters being passed are : 1 11 2010 10 0   
2 - End Parameters being passed are : 1 11 2010 10 5

第二个环境中唯一的不同是该日期范围没有可用的数据。但该错误似乎与数据无关。

任何想法表示赞赏。

This view is running fine on plain pyton/Django/mysql on Windows
I'm porting this to run over jython/Django/mysql and it gives error -

Exception received is : error setting index [10] [SQLCode: 0]    
Parameter index out of range (10 > number of parameters, which is 0). [SQLCode: 0],  
[SQLState: S1009]

The Query is -

cursor.execute("select value from table_name    
where value_till_dt >= str_to_date('%s,%s,%s,%s,%s', '%%m,%%d,%%Y,%%H,%%i')    
AND value_till_dt <=  str_to_date('%s,%s,%s,%s,%s', '%%m,%%d,%%Y,%%H,%%i')    
and granularity='5'    
ORDER BY value_till_dt",    
[int(tempStart.month),int(tempStart.day), int(tempStart.year), int(tempStart.hour), int(tempStart.minute),    
int(tempEnd.month), int(tempEnd.day), int(tempEnd.year), int(tempEnd.hour), int(tempEnd.minute)])

As you see there are 10 parameters being passed to this query.
Does the error mean that the query is not getting the parameters ?

I have printed out the parameters just before the execution and they are showing as being passed correctly -

1 - Start Parameters being passed are : 1 11 2010 10 0   
2 - End Parameters being passed are : 1 11 2010 10 5

The only different in the second environment is that there is no data available for this date range. But the error does not seem to be related to data.

Any thoughts are appreciated.

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

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

发布评论

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

评论(2

來不及說愛妳 2024-08-28 09:39:05

确实是参数样式问题。你必须使用?而不是 %s。

以下是重现错误的方法:

shell> jython
>>> from com.ziclix.python.sql import zxJDBC
>>> (d, v) = "jdbc:mysql://localhost/test", "org.gjt.mm.mysql.Driver"
>>> cnx = zxJDBC.connect(d, None, None, v)
>>> cur = cnx.cursor()
>>> cur.execute("SELECT %s", ('ham',))
..
zxJDBC.Error: error setting index [1] [SQLCode: 0]
Parameter index out of range (1 > number of parameters,
  which is 0). [SQLCode: 0], [SQLState: S1009]

现在,如果您在 ? 标记周围使用引号,您将遇到同样的问题:

>>> cur.execute("SELECT '?'", ('ham',)) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zxJDBC.Error: error setting index [1] [SQLCode: 0]
Parameter index out of range (1 > number of parameters,
  which is 0). [SQLCode: 0], [SQLState: S1009]

重点是不要使用引号并让数据库接口为您完成此操作:

>>> cur.execute("SELECT ?", ('ham',))  
>>> cur.fetchall()
[(u'ham',)]

这是我将如何在代码中做到这一点。首先,创建要用于 str_to_date() 函数的字符串,如下所示:

start = "%d,%d,%d,%d,%d" % (int(tempStart.month),
  int(tempStart.day), int(tempStart.year),int(tempStart.hour), 
  int(tempStart.minute))
stop = "%d,%d,%d,%d,%d" % (int(tempEnd.month),
  int(tempEnd.day), int(tempEnd.year), int(tempEnd.hour),
  int(tempEnd.minute))

创建 SELECT 语句,但不使用任何引号,并将其传递给游标。数据库接口将为您完成这项工作。此外,我们将“粒度”值作为参数。

select = """SELECT value FROM table_name
  WHERE value_till_dt >= str_to_date(?, '%%m,%%d,%%Y,%%H,%%i')
  AND value_till_dt <= str_to_date(?, '%%m,%%d,%%Y,%%H,%%i')
  AND granularity=?
  ORDER BY value_till_dt
"""
cursor.execute(select, (start,stop,5))

我希望这有帮助!

It's indeed a parameter style problem. You have to use ? instead of %s.

Here is how you reproduce the error you are getting:

shell> jython
>>> from com.ziclix.python.sql import zxJDBC
>>> (d, v) = "jdbc:mysql://localhost/test", "org.gjt.mm.mysql.Driver"
>>> cnx = zxJDBC.connect(d, None, None, v)
>>> cur = cnx.cursor()
>>> cur.execute("SELECT %s", ('ham',))
..
zxJDBC.Error: error setting index [1] [SQLCode: 0]
Parameter index out of range (1 > number of parameters,
  which is 0). [SQLCode: 0], [SQLState: S1009]

Now, if you use quotes around the ?-mark, you'll get the same problem:

>>> cur.execute("SELECT '?'", ('ham',)) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
zxJDBC.Error: error setting index [1] [SQLCode: 0]
Parameter index out of range (1 > number of parameters,
  which is 0). [SQLCode: 0], [SQLState: S1009]

The point is to not use quotes and let the database interface do it for you:

>>> cur.execute("SELECT ?", ('ham',))  
>>> cur.fetchall()
[(u'ham',)]

Here is how I would do it in the code. You first make the strings you are going to use for the str_to_date() functions like this:

start = "%d,%d,%d,%d,%d" % (int(tempStart.month),
  int(tempStart.day), int(tempStart.year),int(tempStart.hour), 
  int(tempStart.minute))
stop = "%d,%d,%d,%d,%d" % (int(tempEnd.month),
  int(tempEnd.day), int(tempEnd.year), int(tempEnd.hour),
  int(tempEnd.minute))

You make the SELECT statement, but don't use any quotes, and pass it on to the cursor. The database interface will do the job for you. Also, we put 'granularity' value as a parameter.

select = """SELECT value FROM table_name
  WHERE value_till_dt >= str_to_date(?, '%%m,%%d,%%Y,%%H,%%i')
  AND value_till_dt <= str_to_date(?, '%%m,%%d,%%Y,%%H,%%i')
  AND granularity=?
  ORDER BY value_till_dt
"""
cursor.execute(select, (start,stop,5))

I hope this helps!

醉酒的小男人 2024-08-28 09:39:05

您确定参数标记是 %s 而不是 甚至是 :parameter 吗?检查 DB- 的 paramstyle 参数API模块来查找。

Are you sure that the parameter marker is %s and not ? or even :parameter? Check the paramstyle argument of the DB-API module to find out.

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