[英]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
相同(即对于您选择的任何日期x
和y
,如果x < y
作为Date
,则x < y
作为String
),您可以简单地将日期存储为数据库中的String
( TEXT
)。
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
. 这为您提供了
Date
的String
表示。 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
类型,但它具有帮助函数,您可以使用这些函数将TEXT
和NUMBER
值解释为语句中的日期。
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.