I am trying to read from SQLite database and the method I am using to get Cursor always returns an empty Cursor.
This is the method:
public Cursor getUrls(int oddelki){
String[] columns = new String[]{SQLiteHelper.RAZRED, SQLiteHelper.URNIK_URL};
String selection = SQLiteHelper.RAZRED+"=?";
String[] selectionArgs = new String[]{oddelki+"%"};
open();
return db.query(
SQLiteHelper.IME_TABELE2,
columns,
selection,
selectionArgs,
null, null, null);
}
In the column "RAZRED", data is saved in this format: 1.A, 1.B, 1.C, 2.A, 2.B etc. so when I read from database, I want all data that has "RAZRED" which starts with 1. That is why I am using the % (oddelki+"%").
Here is how I then use the method to read data:
Cursor c = new DatabaseHandler(getApplicationContext()).getUrls(1);
if (c.moveToFirst())
{
do {
Log.i("TAG", c.getString(c.getColumnIndex(SQLiteHelper.RAZRED)));
} while (c.moveToNext());
}
But cursor never moves to first row. The IF condition always fails. There is data in the database that starts with 1. I've checked multiple times, I'm sure of it. The cursor shouldn't return empty.
In the column "RAZRED", data is saved in this format: 1.A, 1.B, 1.C, 2.A, 2.B etc. so when I read from database, I want all data that has "RAZRED" which starts with 1. That is why I am using the % (oddelki+"%").
Then use the LIKE
operator and not =
equality. Change
String selection = SQLiteHelper.RAZRED+"=?";
to
String selection = SQLiteHelper.RAZRED+" LIKE ?";