<p>这确实是一个参数样式问题。你要用吗?而不是%s</p>
<p>以下是如何重现所得到的错误:</p>
<pre><code>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]
</code></pre>
<p>现在,如果你用引号括起来?-马克,你会遇到同样的问题:</p>
^{pr2}$
<p>关键是不要使用引号,而是让数据库接口为您完成:</p>
<pre><code>>>> cur.execute("SELECT ?", ('ham',))
>>> cur.fetchall()
[(u'ham',)]
</code></pre>
<p>下面是我如何在代码中做到这一点。首先,将要用于str_to_date()函数的字符串如下:</p>
<pre><code>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))
</code></pre>
<p>创建SELECT语句,但不使用任何引号,并将其传递给光标。数据库接口将为您完成这项工作。另外,我们将“粒度”值作为参数。在</p>
<pre><code>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))
</code></pre>
<p>我希望这有帮助!在</p>