I have a question. In my app I have saved in my database some lists. Each list has asociated a date in this format 6-June-2011. How can I order these lists by date? I wrote I function like that :
public Cursor getAll(){
return (mDb.rawQuery("SELECT _id, Title, Shop , Data , Budget_allocated ," +
" Budget_spent FROM Lists ORDER BY Data",null));
}
but it doesn't work fine. I think it compare only the day. For example, if I have 31-May-2011 and 6-June-2011, it will say that the first date is after the second date.
It is possible what I am trying to do? Should I modify the date in format like this :6-06-2011?
Thanks..
I don't know anything about android development, but it sounds like your date field is stored as a string rather than a date, is that correct?
If so, you can either:
Change your table so that field is a date (then it should compare correctly)
Or store it in a standard format such that the default string comparison sorts dates correctly. Since string comparison sorts first on first character, second on second character, etc, this would be putting the biggest time difference first, ie. "2011-05-31" (include the zeroes).
(You should be able to convert the field by making a new field, copying the data from the old field into the new field in the correct format, and then deleting the old field and renaming the new one, or more simply if you simply want to change the format of the text. You should be able to do this either from code, or with an "update" query, AFAIK.)
Use a date format as follows...yyyy-MM-dd HH:mm:ss. This is guaranteed to be sortable in ascending or descending order.
If your Data column is not of type datetime there may be an issue. Take a look at this related SO question and answers:
SQL ORDER BY date problem
Related
I am new at Android and also at StackOverflow as a registered user. I have been using this site a lot, but this is the first time that I need help and I could not find an answer.
I need to show in a ListView the diference in days between today and the date picked from a datepicker. I have the data in a SQLite data base. Where should I do the maths for this? In my CursorAdapter Class, or in the Cursor?
Because I need to change the color of the text if the expiration date for doing something is less than 2 days for instance. And that is why I need to do this on my listView to show the user which activities are close to be expired.
I will really appreciate any help. Thanks in advance!
Gretel
Let Sqlite do the math for you by calculating the date difference directly in the database query.
Something like this:
SELECT myNameCol as name, julianday('now') - julianday(myDateColumn) as dayDiff FROM MyTable;
The returned value for dayDiffis the difference in days from now and the value in the myDateColumn column.
I'm developing sms APP and want to receive sms from the specific numbers. But number can be changed sometime with country code as +923201234567 or sometime without country code 03201234567 how I can compare number from database? because don't know in which format number is saved in database(with country code or without country code)
public boolean isMember(String phone, long id){
String query = "SELECT * from members where phone = ? AND active = 1 AND gid = ?";
Cursor c = dbActions.rawQuery(query, new String[]{String.valueOf(phone), String.valueOf(id)});
return c.moveToFirst();
}
Suppose if the number is saved in database without country code 03201234567 then my requirement is to get true if I compare it with country code. +923201234567. Country code could be changed.
PhoneNumberUtils.compare(); is not useful because it not compare with database.
If you can't acquire the correct information always; then you need to look into heuristics.
Meaning: you could write your own comparisons; and when you encounter two numbers like:
03201234567
+923201234567
you can figure: their "tail" is equal; the only difference is that the first one starts with 0 (so no country code) and the second one with +92. So it might be reasonable to declare those two numbers to be equal.
So a "solution" would do things like "normalize" your input (remove all non-digit content; except for leading + signs); and to then make such "tail-bound" comparisons.
If that is "too" fuzzy; I guess then you should step back and describe the requirement that you actually try to resolve here. Why are you comparing numbers; and what do you intend to do with the output of that comparison?!
Normalize all of the phone numbers into the same format before you put them into the database. That way you can just do a normal db search.
The other thing I've done for phone numbers is to convert all letters into the appropriate number, then remove all non digits, then just compare the last 7 digits.
I use Parse.com as backend, and I want work with calendarview in order to let the users select a range of dates and store them in parse. Then I will retrieve that information to set it in a differen CalendarView. I am using CalendarListView explained here
Now in the sample they provide I see this method:
#Override
public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays)
{
Log.e("Date range selected", selectedDays.getFirst().toString() + " --> " + selectedDays.getLast().toString());
}
So if I want to store that in parse, should I use an Array, how do I store secteddays in parse.
There is another library that I was trying to use, here which is similar to the previous and its method to get selected days is:
#Override
public void alertSelectedFail(FailEven even) {
Toast.makeText(context, "alertSelectedFail", Toast.LENGTH_SHORT).show();
}
So As you can see I can get a List of dates from this method, so how do I storage that in Parse? Should I convert that in Array? if yes How do I do it?
And once I do it I want to retrieve that and set it in a different Calendarview just to show. Plaese I am new at android, hope somebody can help.
The selectedDays returns SelectedDays<CalendarDay> That contains only the first and last CalendarDay
Seems the solutions is to manually get all days in between. See genarate dates between two date in android answer to get a List<Date>
Then if you want to make an array out of that you can use the following:
dates[] array = new Date[list.size()];
list.toArray(array); // fill the array
Maybe you also need DateFormatter to correctly formate the date. Or once you have the List<Date> You can also just loop through the list and save them in a String array one by one by using the toString() method on the date.
I am using parse.com in one of my app. I can insert/fetch data to/from parse. Everything works fine. But I want use createdAt field to fetch data.
I have two queries.
Fetch records which are created today.
Fetch records which are created before today.
Problem is parse compares createdAt fields using date-time so I can not use new Date() in query parameter. Is there any way to compare createdAt field by date only?
This question is similar to mine. But is there any standard method ?
As I understand you, You need function something like a keyword "like" which is used in Database to check if a substring exists in a column. So, we do have a method in Parse's Android APIs.
For that you need to call whereContains(String key, String substring)
which will add a constraint for finding string values that contain a provided string.
So, you can write something like this:
query.whereContains("createdAt", "25-05-2015");
That'll only retrieve those data which fall on this date and won't include time.
Doc Reference: https://www.parse.com/docs/android/api/#ParseQuery/whereContains
EDIT:
As createdAt column is Date type, It'll accept a Date variable only. In that case above mentioned function won't work. So, achieving that we will have to use whereGreaterThan and whereLessThan. Sample code snippet is:
Date midnight = new Date();
midnight.setHours(0);
midnight.setMinutes(0);
midnight.setSeconds(0);
Date elevenfiftynine = new Date();
elevenfiftynine.setHours(23);
elevenfiftynine.setMinutes(59);
elevenfiftynine.setSeconds(59);
query.whereGreaterThan(Constants.CREATED_AT_KEY, midnight);
query.whereLessThan(Constants.CREATED_AT_KEY, elevenfiftynine);
Reference: https://www.parse.com/questions/android-api-query-to-get-all-objects-created-today
I having a difficulty with regex, I have a table products that of one of it`s field is a period.
I want to filter records by a period the user entered in my application.
for example the period might be 2006-2012 or just 2006-
I want to filter all the records that contain only the pattern 2006-
I tried many examples but unfortunately nothing works properly
this is my code
Cursor c = rDB.rawQuery("select title,price from Products,UserProducts where UserProducts.product_code=Prodcuts.product_code and USER_EMAIL=? and Products.period like '%[0-9]+%' group by UserSeries.product_code order by Products.title asc", new String[]{email});
while(c.moveToNext())
{
//save the records
}
Products.period like '%[0-9]+%'
LIKE does not work with regexps.
To use regular expression matching, use the REGEXP operator. Note that by default the REGEXP operator is not implemented, even though the syntax supports it.
For simpler matching needs you can also consider GLOB.
I want to filter all the records that contain only the pattern 2006-
You don't need a regular expression for that.
Products.period like '2006-%'
will match anything where period starts with the string 2006-.
I meant any year with "-" symbol at the end
Products.period like '%-'
will match anything where period ends with the string -.