为了账号安全,请及时绑定邮箱和手机立即绑定

Python sqlite比较查询中存储为文本的日期

Python sqlite比较查询中存储为文本的日期

慕尼黑的夜晚无繁华 2022-03-09 20:20:27
我正在使用 Python 并在 sqlite 中构建了一个表。我没有意识到 Python 有一个用于 sqlite 的时间戳字段类型,所以我将它存储为文本。下面是我的代码和结果.. 表中显示的唯一日期是 for2019-06-18但查询返回2019-06-19...不知道为什么import sqlite3# Connecting to the database filesqlite_file = 'insta.sqlite'conn = sqlite3.connect(sqlite_file)c = conn.cursor()# Get all rowsc.execute("SELECT * FROM daily_counts")count_row = c.fetchall()for row in count_row:    print(row)# Get Daily Countc.execute('SELECT count FROM daily_counts where current_date = ? and client_id = ? and metric = ?',    ('2019-06-18', 0, 'C'))count_row = c.fetchall()print(count_row)# Get Daily Countc.execute('SELECT count FROM daily_counts where current_date = ? and client_id = ? and metric = ?',    ('2019-06-19', 0, 'C'))count_row = c.fetchall()print(count_row)# Get Daily Countc.execute('SELECT count FROM daily_counts where current_date = ? and client_id = ? and metric = ?',    ('2019-06-20', 0, 'C'))count_row = c.fetchall()print(count_row)结果:(请注意,当条件为 19 时,它返回 18 的结果,而 18 则不返回任何结果..)(0, u'2019-06-18', u'A', 2)(0, u'2019-06-18', u'B', 6)(1, u'2019-06-18', u'C', 180)(1, u'2019-06-18', u'D', 258)(1, u'2019-06-18', u'E', 111)(0, u'2019-06-18', u'C', 180)(1, u'2019-06-18', u'F', 3)[][(180,)][]我假设我应该使用时间戳字段类型而不是文本?但我仍然希望这能奏效
查看完整描述

1 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

current_date是一个sqlite 保留字

将双引号或反引号添加到"current_date"sql 字符串中的列名(例如 ),您应该会得到预期的结果。


查看完整回答
反对 回复 2022-03-09
  • 1 回答
  • 0 关注
  • 228 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号