[英]SQL strftime with python and sqlite3 returns None
我遇到类似此主题的问题。 并且我尝试了'unixepoch',但是即使如此,我的记录集仍返回None
直接在sqliteman中运行以下sql,我得到的回报是我期望的值
SELECT sum(value)
FROM customers
WHERE customer_id = 1
AND devolution = 'No'
AND strftime('%Y', delivery_date) IN ('2016')
但是,当在python中时,我从print self.rec[0]
返回无。
def get_total_from_last_year(self):
self.now = datetime.datetime.now()
self.lastYear = self.now.year -1
self.con = sqlite3.connect(database.name)
self.cur = self.con.cursor()
self.sql = """SELECT sum(value)
FROM customers
WHERE customer_id=?
AND devolution='No'
AND strftime('%%Y',delivery_date) IN(%s) """ %(str(self.lastYear))
self.cur.execute(self.sql, str(customerID))
self.rs = self.cur.fetchall()
for self.rec in self.rs:
print self.rec[0]
有人可以帮我吗? 非常感谢你。
您在python中的查询字符串是错误的:
现在应该可以使用:
self.cur.execute("SELECT sum(value) \
FROM customers \
WHERE customer_id=? \
AND devolution='No' \
AND strftime('%Y',delivery_date) IN (?)", (customerID, self.lastYear))
您不需要将customerID转换为str(),sqlite3应该可以为您完成此操作。
请注意,为了清楚起见,我在行的末尾添加了\\(因为我在破坏行),您可能不需要它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.