So I’ve been working on setting up an SQL Databse of words, which as far as I can tell from debug logs, actually does load and set up properly. However, when I try to query it, regardless of whether it is in the process of set-up or already done, my program is forced closed because of an exception. Investigation of the log reveals this:
07-12 13:47:02.000: W/dalvikvm(5247): threadid=1: thread exiting with uncaught exception
(group=0x40a401f8)
...
07-12 13:47:02.010: E/AndroidRuntime(5247): Caused by:
android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of
range: handle 0x14a6548
...
07-12 14:41:36.250: E/AndroidRuntime(6326): at com.example.wordsearchandroid.WordListProvider.query(WordListProvider.java:70)
A search of SQLiteBindOrColumnIndexOutOfRangeException on the Android API reveals a fairly… spartan page (http://developer.android.com/reference/android/database/sqlite/SQLiteBindOrColumnIndexOutOfRangeException.html) that mentions nothing of what the “handle” refers to.
Unfortunately, as far as I can tell, nothing in my queries is referring to non-existent columns. This is my query method in my ContentProvider:
public class WordListProvider extends ContentProvider {
private WordListOpenHelper listOpenHelper;
....
public static final String ID = "_ID";
public static final String WORD = "WORD";
public static final String LENGTH = "LENGTH";
...
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
qBuilder.setTables(DB_NAME);
Cursor c;
switch(matcher.match(uri)) {
case SEARCH_WORD_LIST:
Log.d("WORDLISTPROVIDER", "Entered Search");
c = qBuilder.query(listOpenHelper.getReadableDatabase(), projection, null,
selectionArgs, null, null, null);
Log.d("WORDLISTPROVIDER", "Query Completed");
if (c == null)
return null;
c.setNotificationUri(getContext().getContentResolver(), uri);
break;
default:
Log.d("WORDLISTPROVIDER", "Default Entered");
throw new IllegalArgumentException("Unknown URI " + uri);
}
Log.d("WORDLISTPROVIDER", "Exited Search");
return c;
}
^The exception occurs at the line
c = qBuilder.query(listOpenHelper.getReadableDatabase(), projection, null,
selectionArgs, null, null, null);
For reference, here is how the projection and selectionArgs strings for this particular call are set:
String[] proj = {
WordListProvider.ID,
WordListProvider.WORD
};
String[] selectionArgs = {""};
And here is the builder for my database, which is an inner class in WordListProvider:
private static class WordListOpenHelper extends SQLiteOpenHelper {
private SQLiteDatabase wordDb;
private Context helperContext;
private static final String WORD_LIST_CREATE = "CREATE TABLE "+ DB_NAME +
" ( " +
ID + " INTEGER PRIMARY KEY, " +
WORD + " TEXT, " +
LENGTH + " INTEGER " +
");";
WordListOpenHelper(Context context) {
super(context, "wordlist", null, 1);
helperContext = context;
Log.d("OPENHELPER", "Constructed");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("OPENHELPER", "Creating");
wordDb = db;
Log.d("OPENHELPER", "DB set");
wordDb.execSQL(WORD_LIST_CREATE);
Log.d("OPENHELPER", "SQL Execed");
loadWordList();
}
private void loadWordList() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
Log.d("OPENHELPER", "Exception Caught");
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
...
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
...
}
}
loadWords does finish eventually, but I get the exception whether the table has finished loading or not (sometimes the table will not have finished loading, I’ll get the exception, and the program will quit. The table will then finish loading). Also, my column names are consistent, at least they outwardly appear to be–and the exception even occurs if I pass a null (aka return all rows) for the projection to the query; Anyone have any ideas of what could be causing the exception, or what the handle means?
Please, check this line in your provider:
You use selectionArgs without selection parameter. If You pass not null selectionArgs query will fail because there is no place where selection arguments can be bind to.
This line should looks like this:
You can’t use selectionArgs without selection, so try to pass
nullinstead of