繁体   English   中英

如何在SQLite中获取今天的日期?

[英]How do I get today's date in SQLite?

我正在尝试使用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                            

除了@ user2993886的答案外2018/01/19在SQLite中不是有效日期。 记录了格式 ,请参见“时间字符串”。

您可以将2018/01/192018-01-19replace(e.Date, '/', '-') 最好永久执行一次update并在插入时使用正确的格式。 使用本机日期格式会更快(可以为查询建立索引),并且将来可以避免此类错误。

而不是使用date('today')您应该使用date('now')

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM