I really need your help.
I have problem getting values from database. I have two editTexts and one Button. When I insert dates and press Button my table opens without data. How can I get for my database to read those editText values?
My code:
String val1=minDate.getText().toString();
String val2=maxDate.getText().toString();
Cursor c=database.rawQuery("SELECT * from TABLE WHERE DATE BETWEEN '"+ val1+"' AND '"+ val2+"' ", null);
Please refer to documentation:
http://developer.android.com/guide/topics/data/data-storage.html#db
To avoid SQL injection, never add parameter to query by concatenating values, To create a query please refer to: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String[],%20java.lang.String,%20java.lang.String,%20java.lang.String%29
Security tip:
http://developer.android.com/training/articles/security-tips.html#ContentProviders
How date are stored in your table ? Which format ?
When you add parameter to your query format must be the same.
If you want to use BETWEEN key word, date must be stored in this format: year-month-date to be sorted by SQLite.
Update 1
You're right change date format to yyyy-mm-dd
You parameters in your query must have same format, of course.
Put query in variable and log it before, as this:
String query = "SELECT * from TABLE WHERE DATE BETWEEN '"+ val1+"' AND '"+ val2+"' ";
Log.d("MyQuery", query);
Cursor c=database.rawQuery(query, null);
Search in logcat TAG "MyQuery" and post value of query
Now replace your code with below and let me know what you got in Logs
Cursor cursor=database.rawQuery("SELECT * from TABLE WHERE DATE >='"+val1+ "' AND <= '"+val2+ "'", null);
if (cursor.moveToFirst()) {
do {
Log.i("cursor_Data",cursor.getString(cursor.getColumnIndex("Date")));
} while (cursor.moveToNext());
}
If above not work for you then please post your TABLE Structure?
Related
I'm comparing two dates in android sqlite database
I stored dates in the format
YYYY-MM-DD
Cursor cursor=db.rawQuery("SELECT * FROM "+tableName+" WHERE SALESDATE BETWEEN '2020-1-01' AND '2020-2-01';",null);
It gives result with dates of month 10, 11 and 12 along with the dates between above specified dates.
I would like to know if it is a bug or is there any mistake in my code.
The problem here is that your date literals are in a non standard (and likely incorrect) format. Appreciate that the following inequality holds true:
'2020-10-01' > '2020-1-01' AND '2020-10-01' < '2020-2-01'
This is true because the text 10 is lexicographically larger than just 1, but also less than 2. To avoid this problem, use proper date literals:
String sql = "SELECT * FROM " + tableName + " WHERE SALESDATE BETWEEN '2020-01-01' AND '2020-02-01';"
Cursor cursor = db.rawQuery(sql, null);
Note that SQLite does not actually have a formal date type. Thus, it is very important to always store your dates in SQLite using a proper ISO format.
You should store your Date as long value in database. Simple new Date().getTime() gives you this value and new Date(long value) returns it back. So you can make such queries easy.
But what I can suggest is to:
Export your table to CSV,
Change the date values to a proper SQLite TimeString and
Re-import the CSV after deleting the original table.
Then, you can run a query like:
SELECT * FROM tableName WHERE SALESDATE BETWEEN '2020-01-01 00:00:00' AND '2020-02-01 23:59:59'
I am creating a sql database in my app and I am following the documentation on the official developer guide of android, at the webpage
http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow.
I don't understand what is the meaning of the FeedEntry.COLUMN_NAME_UPDATED value.
What should is value be? What does it mean actually?
It's the name of the update column in the feed table ;)
// How you want the results sorted in the resulting Cursor
String sortOrder = FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor cursor = db.rawQuery("SELECT * FROM expenses WHERE expense_date > (SELECT DATE('now', '-7 day'));", null);
I have this above query to get all records that inserted in last 7 days. But as a result of this query i am getting all the records. The date column of these records is inserted via Android's DatePicker and they are in 30-Jul-2015 format. I guess that's why this query does not work properly. Is there a way to make them consistent to get this query work properly or i am missing out something else ? Any help would be appreciated.
I've never tried using the SQLite built in date functions. Taking a quick look at the SQLite date function documentation here, it would seem that to use them the date needs to be in the format YYYY-MM-DD, which would be why your query is failing.
You can do some string manipulation/formatting before saving to the db and use that format.
Personally, I usually format in that manner but without the dash separators, then a simple less than/greater than comparison can be used, treating the date like a regular number. Of course some conversion back and forth is necessary for display, but for under the hood it works great.
int now = 20150730;
String query = "SELECT * FROM expenses WHERE expense_date > '" + now-7 + "'";
I am trying to update a DATETIME column in a Sqlite table using the following query.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.rawQuery(query, null);
I have tested this on both device and emulator but its not working, its not updating the value in the column.
PS: I tried the same query in Sqlite browser and its working there.
Please help me figure out a solution.
Thanks.
Instead of using rawQuery(), use execSQL() if the execution doesn't return any data.
String query = "UPDATE myTable SET displayed = datetime('now') where _id = " + id;
database.execSQL(query);
Though just a tip, it is better to use UNIX time if you really want to store and process date in the future. It will be more flexible and standardized.
Im using SQLite as data storage for my Android App. I have created one table with column of type datetime. When I do the insert of records or selects statements I use the format dd.MM.yyyy (10.08.2012) for dates.
Now, I have issue with getting the row with the latest / MAX date. I have tried using the MAX(date_column) statement, but it returns wrong date back. I have following dates in my table and it returns 31.07.2012 in stead of 04.08.2012. Anyone know what I'm doing wrong?
04.08.2012
03.08.2012
02.08.2012
01.08.2012
31.07.2012
30.07.2012
Here is part of the code:
String selection = "measurementDate = (SELECT MAX(measurementDate) FROM Measurements)";
Cursor cursor = database.query("Measurements", allColumns, selection, null, null, null, null);
cursor.moveToFirst();
...
Could it be because of 31?
Try to see if this helps:
SELECT measurementDate FROM Measurements ORDER BY measurementDate desc LIMIT 1
It's probably due to the comparison being made on the stored string rather than treating it as a date.
You could store the string in differently, YYYYMMDD, which should compare them correctly or, if you can modify the table, store the dates as milliseconds and format back to a string when needed.
This might help