数据库
首页 > 数据库> > android-在SQLite中选择日期大于或等于今天的位置

android-在SQLite中选择日期大于或等于今天的位置

作者:互联网

我的日期以dd / mm / yyyy格式存储在数据库中,并且我只想显示日期等于或大于今天的项目.

我尝试了WHERE date(fixtures.date)> = date(‘now’),但没有任何结果.

下面是我的查询,任何帮助将不胜感激

SELECT fixtures.id, 
       fixtures.team_1_id, 
       fixtures.team_2_id, 
       fixtures.date, 
       fixtures.time, 
       fixtures.pitch, 
       teams.team_name, 
       teams_1.team_name AS awayTeam, 
       leagues.league_name 
FROM   fixtures 
       INNER JOIN teams 
               ON fixtures.team_1_id = teams.id 
       INNER JOIN teams AS teams_1 
               ON fixtures.team_2_id = teams_1.id 
       INNER JOIN teams_vs_leagues 
               ON teams.id = teams_vs_leagues.team_id 
       INNER JOIN leagues 
               ON teams_vs_leagues.league_id = leagues.id 
WHERE  date(fixtures.date) >= date('now') 
ORDER  BY fixtures.date DESC 

解决方法:

为什么说日期以dd / mm / yyyy格式存储?根据SQLite docs

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  1. TEXT as ISO8601 strings (“YYYY-MM-DD HH:MM:SS.SSS”).

  2. REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.

  3. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

基本上,此查询将不起作用

select 1 where '01-08-2016' >= date('now')

但这会

select 1 where '2016-08-01'>= date('now')

因此,您可以验证您的日期fixtures.date是否格式为“ yyyy-MM-dd”,否则查询将无法进行.另外,请记住使用date(‘now’,’localtime’)来获取您的本地时间.

如果您的日期格式正确,则可以尝试执行以下操作

SELECT fixtures.id
FROM   fixtures
WHERE  fixtures.date >= date('now')

如果您确实得到了结果,则联接不匹配任何行.

有关更多信息,请检查此answer

标签:android-studio,sqlite,android-sqlite,android
来源: https://codeday.me/bug/20191027/1941888.html