Can't match string which stored in SQLite [duplicate] - android

This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
In part of my application I get date from mobile and store it in SQLite Database as string.
when I try to select rows from database which have same date, can't find any match result.
Also as in below code, I tried to get specific cell of table which I'M sure it's match the current date. but no match.
is there any idea?
Get Date from mobile and insert it as string.
String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
MyDB.insertRow(currentDateString);
Check if date from database equal mobile date.
if (MyDB.getRow(21).getString(1).toString()==currentDateString)
Toast.makeText(getBaseContext(),"Match",Toast.LENGTH_SHORT).show();

You don't match Strings in Java using == operator. You use .equals() method as String is not a primitive type.
So try this -
if (MyDB.getRow(21).getString(1).toString().equals(currentDateString))

Strings aren't matched by == operator because it is an object.
try
MyDB.getRow(21).getString(1).toString().equals(currentDateString)
and if you need it not case sensitive
MyDB.getRow(21).getString(1).toString().equalsIgnoreCase(currentDateString)
Also
make sure that the inserted date format is the same that you match with

Related

How can i store more than 24,00,000 character in sqlite? [duplicate]

This question already has an answer here:
sqlite get field with more than 2 MB [duplicate]
(1 answer)
Closed 5 years ago.
I store Base64 String in sqlite and for that I use these data types TEXT, MEDIUMTEXT and LONGTEXT. But when I cross limit of 23,00,000 character, Android is not able to read data of the table. Can anyone help me?
You can always create code to break Strings apart at specific locations, and stitch it back together if you need to read the whole string.
String string = "abc def ghi jkl mno";
String ss[] = s.split(" ", 5);
// ss = {"abc", "def", "ghi", "jkl", "mno"};

How to store array of integer in sharedPreferences [duplicate]

This question already has answers here:
Save ArrayList to SharedPreferences
(38 answers)
Closed 6 years ago.
I have an array integer in android studio like this { 0 , 0 , 0 , 1 , 1 , 0 , 1}
how can I store and recall it in shared preferences
There is no way you can store an array as it is.
Either you can convert it to JSON String and store it as a string.
Or you can store each element of the array individually with an ID like this.
Build a string out of it. Preferably using StringBuilder.
And, when reading, just split() on commas and then user Integer.parseInt() on all elements of the array.
Should be faster than the json route and does not require any extra dependency.

compare createdAt field with date only (not datetime) on parse.com in android

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

How to extract the string variable in android? [duplicate]

This question already has an answer here:
How to extract this string variable in android?
(1 answer)
Closed 9 years ago.
String test=["1","Low-level programming language",true]
Here i want to extract this string value and as i need to get only second value like "Low-level programming language".How to get this value using string functions in android?
Per your comment, I'm assuming that you have a single string that contains the entire text (including the brackets). In general, splitting comma-separated values is a fairly tricky process. For your specific string, though, it's kind of easy:
String test = "[\"1\",\"Low-level programming language\",true]";
String[] pieces = test.split(",");
String middle = pieces[1];
// now strip out the quotes:
middle = middle.substring(1, middle.length() - 1);
In general, you might want to look at using a general CSV parser like Apache Commons CSV or openCSV.
Alternatively, if this is JSON data (which looks more likely than CSV), take a look at using one of the Java JSON libraries listed here (scroll down the page to see the list).

Sort lists by date?

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

Categories

Resources