【发布时间】:2014-12-28 15:24:06
【问题描述】:
我有以下功能:
for i in range(1,13):
q_totes_1="""SELECT sum(kaxia) FROM es_data WHERE strftime('%%m',es_date)='%s' AND es_orig=1"""%(str(i))
self.cur.execute(q_totes_1)
m_totes_1=self.cur.fetchone()[0]
print q_totes_1
if m_totes_1 is None:
m_totes_1=0.0
当我知道我应该有另一个结果时,它总是返回 None。从print q_totes_1 我得到了我直接在sqlite 上执行的查询,我得到了想要的结果。所有导入都是正确的,因为我已经在同一类的其他函数中成功使用了它们。
我尝试运行没有strftime('%%m',es_date)='%s' 部分的类似查询,它运行正常。
有人可以告诉我我缺少什么吗?
【问题讨论】:
-
你的意思是 它总是返回 None ?您是否将此代码定义为函数?
-
@Kasra
m_totes_1为无;我也尝试了self.cur.fetchall(),得到了(None,) -
@Filippos 使用单个 % 并检查
-
@Bhargav Rao 我得到
ValueError: unsupported format character 'm' (0x6d) at index 48 -
@Filippos Final
"""SELECT sum(kaxia) FROM es_data WHERE strftime('%%m',es_date)= ? AND es_orig=1""",(str(i),)