Anete Jonase
Anete Jonase

Reputation: 13

Select date range from sqlite

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);

Upvotes: 0

Views: 2658

Answers (2)

IshRoid
IshRoid

Reputation: 3735

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?

Upvotes: 1

LaurentY
LaurentY

Reputation: 7653

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

Upvotes: 1

Related Questions