Reputation: 1847
I've been struggling a while with querying my SQLite database using one of my table field. I successfully query using the ForeignId
field, but not with the Available
field of my table QuizTable
. I was hoping you could point out something I missed. Here is the call I make to the function queryQuiz
. Please note the whereClause
(kind of cut off) which is supposed to filter the available
field for a value of 1
. (side note: what if you want to query for different data types?)
QuizCursorWrapper cursor = db.queryQuiz(QuizTable.Cols.AVAILABLE + "=?", new String[]{"1"});
This is my queryQuiz function in a class that extends SQLiteOpenHelper
.
Here's the error I am getting from calling queryQuiz
:
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
public QuizCursorWrapper queryQuiz(String whereClause, String[] whereArgs){
Cursor cursor = db.query(
QuizTable.NAME,
null,
whereClause,
whereArgs,
null,
null,
null
);
return new QuizCursorWrapper(cursor);
}
my QuizCursorWrapper
class
public class QuizCursorWrapper extends CursorWrapper {
public QuizCursorWrapper(Cursor cursor){
super(cursor);
}
public String getQuiz(){
String paragraph = getString(getColumnIndex(QuizSchema.QuizTable.Cols.PARAGRAPH));
return paragraph;
}
public String getQuizUUID(){
String uuid = getString(getColumnIndex(QuizSchema.QuizTable.Cols.UUID));
return uuid;
}
}
and finally, my QuizSchema
which defines the fields in my table.
public class QuizSchema {
public static final class QuizTable{
//nested class
public static final String NAME = "quizTable";
public static final class Cols{
public static final String SCORE = "score";
public static final String PARAGRAPH = "paragraph";
public static final String FOREIGNID = "foreignId";
public static final String UUID = "uuid";
public static final String AVAILABLE = "available";
}
}
}
I am very confused because I can clearly see rows with available
fields equal to 1 in my quiz table . Sorry for the broad question, it's just that I can't put a finger on a problem and the debugger isn't too helpful. Please let me know if you need more info.
Upvotes: 1
Views: 46
Reputation: 56943
I believe that your issue is how column storage class/type affinity effects comparisons.
The solution is force the type affinity and thus how the comparisons are made. Forcing the type affinity can be achieved using CAST to set the type affinity.
As such a solution could be to CAST the values to type INTEGER for the comparison. You could do this by using :-
QuizCursorWrapper cursor = db.queryQuiz("CAST(" + QuizTable.Cols.AVAILABLE + " AS INTEGER) =CAST(? AS INTEGER)", new String[]{"1"});
You may wish to have a read of :-
Upvotes: 2