带有 strftime 的 sqlite 查询总是返回 None [英] sqlite query with strftime always returns None
问题描述
我有以下功能:
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 上执行的查询,我得到了想要的结果.所有导入都是正确的,因为我已经在同一个类的其他函数中成功使用了它们.
It always returns None while I know that I should have another result. From the print q_totes_1
I get the query which I execute straightly on sqlite and I get the desired result. All the imports are correct as I already have used them successfully in other functions of the same class.
我尝试在没有 strftime('%%m',es_date)='%s'
部分的情况下运行类似的查询,并且运行正常.
I tried running a similar query without the strftime('%%m',es_date)='%s'
portion and it run correctly.
有人可以告诉我我错过了什么吗?
Can somebody give me a hint of what I'm missing?
推荐答案
最后我得到了要执行的查询,但令我失望的是它非常简单:
Finally I got the query to execute and to my disappointment it was annoyingly simple:
查询应该具有这种格式(我倾向于忽略注入的可能性,因为与数据库的唯一交互将通过 python 应用程序):
The query should have this format (I tend to ignore the possibility of injections as the only interaction with the DB will be through the python app):
SELECT sum(kaxia) FROM es_data WHERE strftime('%%m',es_date,'unixepoch')= '%s'AND es_orig=1"""%(str(i))
缺少 'unixepoch'
修饰符似乎输出了 None
值结果.
The lack of the 'unixepoch'
modifier seemed to output a None
value result.
非常感谢所有试图提供帮助的人...
Many thanks to all who tried to help...
这篇关于带有 strftime 的 sqlite 查询总是返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!