从数据库中检索的Android最新明智数据 [英] Retrieve date wise data from database in Android
问题描述
在我的数据库,我有1表日期
为一体的列,它存储的日期,如果用户在我的表中添加一些数据。
In my database I have 1 table with date
as one of the column, which stores the date when user add some data in my table.
现在我要检索当月单独的数据。我搜索通过谷歌却没有得到相应的答案。
Now I want to retrieve the data for the current month alone. I searched through Google but didn't get appropriate answer.
我的表创建code:
"create table incomexpense(
_id integer primary key autoincrement,
"+"price text not null,description text not null,
"+"quantity text not null,
"+"total text not null,
"+"category text not null,
"+"recurrence text not null,
"+"date text not null,
"+"groups text not null,
"+" recurrenceid text not null);";
任何人都可以请帮助如何获得的第7天的时间形成当前的日期。
Can anyone please help how to get the date of 7th day form the current date.
推荐答案
如果您想为自动加载当前年/月,使用SQLite的的strftime
函数现在
修改和放大器; '%Y-%M
格式掩码。
If you want to "automatically" load the current year/month, use SQLite's strftime
function with 'now'
modifier & '%Y-%m'
format mask.
select strftime('%Y-%m', 'now');
现在,来到你的问题:
现在我要检索的数据当月孤单。
Now I want to retrieve the data for the current month alone.
好了,来了一个烂摊子。您不能存储日期为datetime型,而不是它存储为一个字符串格式的SQLite无法解析使用日期函数。所以,你必须把它转换成的SQLite能够理解的格式。
Well, here comes a mess. You can't store the date as a datetime type, instead it's stored as a string in a format SQLite can't parse using date functions. So you'll have to convert it into a format which SQLite can understand.
您可以做到这一点通过使用常规的 SUBSTR
功能
You can do that by using the usual substr
function
substr(exp_date, 7) || '-' || substr(exp_date, 4,2) || '-' || substr(exp_date, 1,2)
因此,这将变相 DD / MM / YYYY
到 YYYY-MM-DD
格式。
所以,完整的查询将是:
So the full query will be:
SELECT *
FROM incomexpense
WHERE Strftime('%Y-%m', 'now') = Strftime('%Y-%m', Substr(exp_date, 7)
|| '-'
|| Substr(exp_date, 4, 2)
|| '-'
|| Substr(exp_date, 1, 2))
请参阅如何工作的在这里。
这篇关于从数据库中检索的Android最新明智数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!