我如何在SQLite中获得今天的约会?

时间:2018-04-09 16:07:43

标签: python sqlite pandasql

我正在尝试使用Pandasql来查询我的数据帧。然而,它给了我一个空的数据框,即使我知道它不应该。我认为这是因为我在where子句中错误地使用了今天的日期。以下是我的代码的一部分:

df_event2import=[]
today = now.strftime("%Y-%m-%d")      
q = """ select e.Date, e.SITA, e.Events from df_event e where e.Date >= date('today') """        
df_event2import=((pandasql.sqldf(q, locals())))
df_all_event2import=df_all_event2import.append(df_event2import,ignore_index=True)
print(df_all_event2import)

这里要求的是一些示例数据。这是我在where子句中使用date(' now')而不是date(' today')后得到的。正如你所看到的,它给了我1月份以后的数据,这不是我想要的,我想要今天的数据。

     Date   SITA                      Events
0    2018/01/01  ABZPD  New Years Day Bank Holiday
1    2018/01/02  ABZPD                            
2    2018/01/03  ABZPD                            
3    2018/01/04  ABZPD                            
4    2018/01/05  ABZPD                            
5    2018/01/06  ABZPD                            
6    2018/01/07  ABZPD                            
7    2018/01/08  ABZPD                            
8    2018/01/09  ABZPD                            
9    2018/01/10  ABZPD                            
10   2018/01/11  ABZPD                            
11   2018/01/12  ABZPD                            
12   2018/01/13  ABZPD                            
13   2018/01/14  ABZPD                            
14   2018/01/15  ABZPD                            
15   2018/01/16  ABZPD                            
16   2018/01/17  ABZPD                            
17   2018/01/18  ABZPD                            
18   2018/01/19  ABZPD                            

2 个答案:

答案 0 :(得分:2)

@user2993886's answer外,2018/01/19不是SQLite中的有效日期。 The formats are documented,请参阅"时间字符串"。

您可以使用2018/01/192018-01-19转换为replace(e.Date, '/', '-')。最好使用update永久执行此操作,并在插入时使用正确的格式。使用本机日期格式会更快(查询可以编入索引),并避免将来出现这种错误。

答案 1 :(得分:1)

您应该使用date('today')

而不是date('now')

SQLite文档中的更多详细信息:

https://www.sqlite.org/lang_datefunc.html

相关问题