gpt4 book ai didi

python - 使用 strftime 的 sqlite 查询始终返回 None

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:59 25 4
gpt4 key购买 nike

我有以下功能:

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' 部分的情况下运行类似的查询,并且运行正确。

有人可以告诉我我缺少什么吗?

最佳答案

更改为

self.cur.execute( """SELECT sum(kaxia) FROM es_data WHERE strftime('%m',es_date)= ? AND es_orig=1""",(str(i),) )

您的代码将是

for i in range(1,13):
x = self.cur.execute( """SELECT sum(kaxia) FROM es_data WHERE strftime('%m',es_date)= ? AND es_orig=1""",(str(i),) )
m_totes_1=x.fetchone()[0]
print q_totes_1
if m_totes_1 is None:
m_totes_1=0.0

您也是 SQL 注入(inject)的受害者。 (在数据库中始终使用 ? 符号)

enter image description here

关于python - 使用 strftime 的 sqlite 查询始终返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com