简体   繁体   English

如何在sqlite数据库中插入数据类型日期(yyyy-MM-dd)并在android中的两个日期之间检索数据

[英]how to insert data type date(yyyy-MM-dd) in sqlite database and retrive data between two dates in android

I have a Sqlite3 database table contains name,address,date of birth details.i want to display 1990-01-01 to 1995-01-01 details. 我有一个Sqlite3数据库表,包含姓名,地址,出生日期详情。我想显示1990-01-01到1995-01-01的详细信息。

but Sqlite3 database stores only following data types. 但Sqlite3数据库仅存储以下数据类型。

TEXT
NUMERIC
INTEGER
REAL
NONE

Any one have some hint to store and retrieve date format data..? 任何人都有一些提示来存储和检索日期格式数据..?

From my own experience on doing several projects with database in Android my answer is: 根据我自己在Android中使用数据库做几个项目的经验,我的答案是:

Do not store the date as a string. 不要将日期存储为字符串。 Never! 决不! Ever! 永远! Store them as Unix timestamps and format them as needed during runtime. 将它们存储为Unix时间戳,并在运行时根据需要对其进行格式化。

the important thing here is to separate what is your data and what is the on-screen representation of your data. 这里重要的是分离您的数据是什么以及数据的屏幕表示。 Storing in a database the on-screen representation of your data is wrong. 在数据库中存储数据的屏幕表示是错误的。

You'll always store your dates as INTEGER types. 您始终将日期存储为INTEGER类型。

So for example to store the date now you'll store the value System.currentTimeInMilis 因此,例如,为了存储日期,您将存储值System.currentTimeInMilis

To select between 1990-01-01 and 1995-01-01 you will: 要在1990-01-01和1995-01-01之间进行选择,您将:

long val1 = new GregorianCalendar(1990, 01, 01).getTimeInMillis();
long val2 = new GregorianCalendar(1995, 01, 01).getTimeInMillis();

and then you'll do the normal SELECT statement between those 2 values. 然后你将在这两个值之间执行正常的SELECT语句。

to show those values in the screen as yyyy-MM-dd you'll use the SimpleDateFormat class: 要在屏幕上显示这些值为yyyy-MM-dd您将使用SimpleDateFormat类:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
long longDate = cursor.getLong(colNumber); // from the database
String stringDate = dateFormat.format(new Date(longDate));

Use this code to convert your date into millisecond format and store it into your database as INTEGER types 使用此代码将日期转换为毫秒格式,并将其作为INTEGER类型存储到数据库中

String someDate = "1995-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(someDate);
System.out.println(date.getTime());

date.getTime()-give the millisecond format date.getTime() - 给出毫秒格式

At the same way to convert your input (ie from 1990-01-01 and to date 1995-01-01) 以同样的方式转换您的输入(即从1990-01-01到1995-01-01)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date1 = sdf.parse(1990-01-01);
    value1=date.getTime();
Date date2 = sdf.parse(1995-01-01);
    value2=date.getTime();

Retrieve from database using following query 使用以下查询从数据库中检索

db.rawQuery("SELECT * FROM table_name WHERE column_name BETWEEN "+value1+" AND "+value2+"",null);

or 

db.rawQuery("SELECT * FROM table_name WHERE column_name<="+value1+" AND column_name>="+value2+"",null);

You can do something like this 你可以做这样的事情

DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
    Date date1=df.parse("1990-01-01");
    Date date2=df.parse("1995-01-01");
    Date myDate=df.parse("1992-01-01"); // checking date

    if((date1.getTime()<myDate.getTime())&&(myDate.getTime()<date2.getTime())){
        System.out.println(df.format(myDate)+" is in this range");
    }else{
        System.out.println(df.format(myDate)+" is not in this range");
    }

Since the format you want to use ( yyyy-MM-dd ) is ordered in the same way as a String (ie for any dates x and y you would choose, if x < y as a Date , then x < y as a String ), you can simply store the dates as String s ( TEXT ) in your database. 由于您要使用的格式( yyyy-MM-dd )的排序方式与String相同(即对于您选择的任何日期xy ,如果x < y作为Date ,则x < y作为String ),您可以简单地将日期存储为数据库中的StringTEXT )。

When selecting the values between them, you would just have to use a WHERE clause in your SELECT statement like this: 在它们之间选择值时,您只需在SELECT语句中使用WHERE子句,如下所示:

 SELECT * FROM yourTable WHERE yourDateFieldName > ? and yourDateFieldName < ?

You can then use DateFormat.format to set the values for the ? 然后,您可以使用DateFormat.format来设置?的值? parameters of your prepared statement. 准备好的陈述的参数。 The first parameter would be the "start" date, and the second would be the "end" date. 第一个参数是“开始”日期,第二个参数是“结束”日期。 You can replace < with <= and > with >= if you want the items on start and end dates included. 如果您希望包含开始日期和结束日期的项目,则可以将< with <=>替换为>=

This gives you a String representation of a Date . 这为您提供了DateString表示。 To convert from that to an actual Date object you can use date formatter's parse method (ie SimpleDateFormat.parse ). 要从它转换为实际的Date对象,您可以使用日期格式化程序的parse方法(即SimpleDateFormat.parse )。

Another, "cleaner", approach would be to use the SQLite date and time functions (see here ). 另一种“更干净”的方法是使用SQLite日期和时间函数(参见此处 )。 While SQLite doesn't have a DATE type for storing date values, it has helper functions that you can use to interpret TEXT and NUMBER values as date in your statements. 虽然SQLite没有用于存储日期值的DATE类型,但它具有帮助函数,您可以使用这些函数将TEXTNUMBER值解释为语句中的日期。

If you don't need extra processing for your date values, I'd recommend going for the first solution as it should be faster because it merely compares TEXT s rather than parsing and extracting a date from them, then comparing the extracted date (I haven't compared the speed of the two approaches, so don't take my word for it on this one). 如果您不需要对日期值进行额外处理,我建议您选择第一个解决方案,因为它应该更快,因为它只是比较TEXT而不是从它们中解析和提取日期,然后比较提取的日期(I没有比较两种方法的速度,所以不要在这一方面接受我的话。 This approach also has less code to write and maintain and the code is easier to read. 这种方法编写和维护的代码也较少,代码更易于阅读。

Sources: 资料来源:

SQLite data type - for the validity of comparing two TEXT values SQLite数据类型 - 用于比较两个TEXT值的有效性

SimpleDateFormat - Android documentation SimpleDateFormat - Android文档

You can use dates in yyyy-MM-dd format directly, JDBC will understand it. 您可以直接使用yyyy-MM-dd格式的日期,JDBC会理解它。 Assuming we a have a table t1 with c1 of DATE type 假设我们有一个表t1,其中c1为DATE类型

PreparedStatement ps = conn.prepareStatement("insert into t1 (c1) values (?)");
ps.setString(1, "2001-01-01");
ps.executeUpdate();

Reading dates is simple too 阅读日期也很简单

ResultSet rs = st.executeQuery("select c1 from t1");
rs.next();
Date date = rs.getDate(1);

ResultSet.getDate returns result as java.sql.Date whose toString method returns date in yyyy-MM-dd format ResultSet.getDate以java.sql.Date形式返回结果,其toString方法以yyyy-MM-dd格式返回日期

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

相关问题 如何从sqlite数据库中以dd / mm / yyyy格式获取两个日期之间的数据 - How to get data between two dates from sqlite database which is in dd/mm/yyyy format 如何使用日期类获取格式为“ YYYY-MM-DD 00:00:00.0”的数据? - How can i get data in format “YYYY-MM-DD 00:00:00.0” using class Date? 比较两个日期与时间,其中Formate是yyyy-MM-dd HH:mm:Android中的ss - Compare two dates with time whose Formate is yyyy-MM-dd HH:mm:ss in Android 在Android中解析yyyy-MM-DD字符串到日期yyyy-MM-DD? - Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android? yyyy-MM-dd&#39;T&#39;HH:mm:ss 和 yyyy-MM-dd&#39;T&#39;HH:mm:ssXXX 之间的日期格式差异 - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX 如何解析 yyyy-MM-dd HH:mm:ss 到日期? - How to parse a yyyy-MM-dd HH:mm:ss to Date? 如何以“ yyyy-mm-dd”格式将日期列表放入JComboBox? - How to put list of dates in JComboBox in 'yyyy-mm-dd' format? 如何使用YYYY-MM-DD格式让SQLite按日期排序? - How can i get SQLite to sort by date properly using YYYY-MM-DD format? 将 dd-MM-yyyy 和 yyyy-MM-dd 解析为 Java 中的日期类型? - Parsing both dd-MM-yyyy and yyyy-MM-dd to Date type in Java? 如何使用Java将mm-dd-yyyy(来自csv文件中的数据)的格式更改为yyyy-mm-dd存储在MySql中 - How to change format of mm-dd-yyyy(of data from csv file) to be stored as yyyy-mm-dd in MySql using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM