热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在androidsqlite数据库中比较日期(存储为字符串)?-Comparedates(storedasstring)inandroidsqlitedatabase?

IstoredatesasStringinmydatabase,inthisformat:我将日期作为字符串存储在我的数据库中,格式如下:YYYY-MM-DDHH:MM:S

I store dates as String in my database, in this format:

我将日期作为字符串存储在我的数据库中,格式如下:

YYYY-MM-DD HH:MM:SS

which is one of supported formats (http://www.sqlite.org/lang_datefunc.html). Now I want to compare these stored dates (well, strings) with another dates. My dates are stored in column column_date, so I'm trying this:

这是一种受支持的格式(http://www.sqlite.org/lang_datefunc.html)。现在我想将这些存储的日期(嗯,字符串)与其他日期进行比较。我的日期存储在列column_date中,所以我正在尝试:

SELECT  * FROM MyTable
WHERE 
(Date(column_date) >= Date (2000-01-01) AND Datetime(column_date) <= Datetime (2050-01-01))

As i read documentation, The date and time functions use a subset of IS0-8601 date and time formats. The date() function returns the date in this format: YYYY-MM-DD. so I suppose, I'm doing it right - I create date from stored string and compare it with date created from another strings.

当我阅读文档时,date和time函数使用IS0-8601日期和时间格式的子集。函数的作用是:返回日期。我想,我做对了,我从存储的字符串中创建日期,并将它与从其他字符串中创建的日期进行比较。

But it doesn't work, even when column_date is date from this year, and as u can see the start and end dates are very benevolent. Tried also this (used datetime instead of date):

但它不起作用,即使当column_date是今年的日期,当u可以看到开始和结束日期是非常仁慈的。还尝试了这个(使用datetime而不是日期):

SELECT  * FROM MyTable
    WHERE 
    (datetime(column_date) >= Date (2000-01-01) AND datetime(column_date) <= Datetime (2050-01-01))

and this (use between instead of <= and >=)

这个(用between而不是<=和>=)

SELECT  * FROM MyTable
    WHERE 
    (Date(column_date) between Date (2000-01-01) AND Datetime (2050-01-01))

and all other possible combinations. What the hell I'm doing wrong, am I stupid, or the documentaion lies, or I missed something very important? Trying to find solution few hours, but nothing works...

以及所有其他可能的组合。我到底做错了什么,我是愚蠢的,还是文件上的谎言,还是我错过了一些非常重要的东西?试着找几个小时的解决方案,但是什么都不做……

5 个解决方案

#1


17  

if you are storing dates as strings in the format

如果将日期存储为格式中的字符串

YYYY-MM-DD HH:mm:ss  

====> then your date field is a DateTime datatype

==== =>那么您的日期字段就是一个DateTime数据类型

Thus you have to use such query

因此,您必须使用此类查询

select * 
  from MyTable 
  where mydate >= Datetime('2000-01-01 00:00:00') 
  and mydate <= Datetime('2050-01-01 23:00:59')

you can also use the snippet given by @Joop Eggen with th between operator it's th same approche.

您也可以使用@Joop Eggen和th之间的操作符,它是相同的方法。

The BETWEEN operator is logically equivalent to a pair of comparisons. "x BETWEEN y AND z" is equivalent to "x>=y AND x<=z" except that with BETWEEN, the x expression is only evaluated once. see sqlite3 docs

BETWEEN运算符在逻辑上等同于一对比较。“x在y和z之间”等价于“x>=y和x<=z”,但x表达式只计算一次。看到sqlite3文档

#2


6  

SELECT *
FROM MyTable
WHERE 
    column_date BETWEEN '2000-01-01 00:00:00' AND '2050-01-01 23:00:59'

Should do the trick. This only uses strings but you said that would be fitting.

应该足够了。它只使用字符串,但你说它是合适的。

The error was the missing time part or not doing a substr on the date part only. And then data/time conversion functions might give wrong things too.

错误是丢失的时间部分或仅在日期部分不执行子str。然后数据/时间转换函数可能也会出错。

#3


3  

I think your issue is here...

我认为你的问题在这里……

AND Datetime(column_date) <= Datetime (2050-01-01))

Why are you using Datetime here instead of Date?

为什么要在这里使用Datetime而不是日期?

Try this:

试试这个:

SELECT  * FROM MyTable WHERE (Date(column_date) >= Date (2000-01-01) AND Date(column_date) <= Date (2050-01-01))

#4


2  

Date filed in the SQLite table will be TEXT and date data should be stored as "2012-12-31 00:12:00" format that comparison dose not give you trouble and if we consider that fields are date_from and date_to and we are storing current date at to_date then we should get the current date as bellow and

SQLite表中的日期提交日期将文本和数据应该存储为“2012-12-31 00:12:00”格式,比较不给你麻烦,如果我们考虑到字段date_from date_to和我们存储当前日期to_date然后我们应该得到当前日期详见下表

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String to_date = sdf.format(new Date());

and the SQL query should be

SQL查询应该是

    crs = db.rawQuery(
      "SELECT _id, date_from, date_to, done_or_not, list_name " +
      "FROM list_tbl " +
      "WHERE " +
      "date_from <= Datetime('"+to_date+"') AND date_to >= Datetime('"+to_date+"')", null);

#5


0  

Compare dates (stored as string) in android SQLite database. Use date in 12/07/2015 format and use a query like this:

比较android SQLite数据库中的日期(以字符串形式存储)。使用日期为12/07/2015,并使用如下查询:

SELECT * FROM tableName WHERE Date >=('12/07/2015') and appointmentDate <('13/07/2015')";

This works for me.

这适合我。


推荐阅读
author-avatar
s350350350
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有