我遇到问题like in this主题。我已经尝试过unixepoch',但即使这样,我的记录集也会返回无
直接在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]
有人可以帮帮我吗?
非常感谢你。
答案 0 :(得分:1)
你在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应该能够为您完成。
请注意,为了清楚起见,我在行尾添加了\'(因为我打破了这些行),你可能不需要它们。