问题
I have implemented a Content Provider, it is resulting in following error
04-25 00:25:16.856: E/AndroidRuntime(520): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x234590
The query in Content Provider is
Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null,
sortOrder);
and call to Content Provider is
cursor = cr.query(uri, new String[] { ContactTableMetaData.ABC,
ContactTableMetaData.MNP }, ContactTableMetaData.XYZ + "=?",
new String[] { "phonenumber1", "phonenumber2", "phonenumber3",
"phonenumber4", "phonenumber5", "email1", "email2", "email3",
"email4", "email5" }, null);
Complete logcat is
04-25 00:25:16.856: E/AndroidRuntime(520): FATAL EXCEPTION: main
04-25 00:25:16.856: E/AndroidRuntime(520): java.lang.RuntimeException: Unable to start activity 04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.os.Looper.loop(Looper.java:123)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 00:25:16.856: E/AndroidRuntime(520): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 00:25:16.856: E/AndroidRuntime(520): at java.lang.reflect.Method.invoke(Method.java:521)
04-25 00:25:16.856: E/AndroidRuntime(520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 00:25:16.856: E/AndroidRuntime(520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 00:25:16.856: E/AndroidRuntime(520): at dalvik.system.NativeStart.main(Native Method)
04-25 00:25:16.856: E/AndroidRuntime(520): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x234590
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:241)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:182)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
04-25 00:25:16.856: E/AndroidRuntime(520): at com.wissenways.helper.VoiceAppContactContentProvider.query(VoiceAppContactContentProvider.java:171)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.content.ContentProvider$Transport.query(ContentProvider.java:163)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.content.ContentResolver.query(ContentResolver.java:245)
04-25 00:25:16.856: E/AndroidRuntime(520): at com.wissenways.checkcontacts.DisplayGreatPeople.onCreate(DisplayGreatPeople.java:45)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-25 00:25:16.856: E/AndroidRuntime(520): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-25 00:25:16.856: E/AndroidRuntime(520): ... 11 more
Thanks.
回答1:
String selection = ContactTableMetaData.XYZ + "=?";
String[] selectionArgs = new String[] { "phonenumber1", "phonenumber2", "phonenumber3",
"phonenumber4", "phonenumber5", "email1", "email2", "email3",
"email4", "email5" }
The amount of ?
must match the amount of selectionArgs
. If you provide 10 items then the selection
has to have 10 ?
.
The N-th ?
in selection
is replaced by the N-th item in selectionArgs
.
Edit: If you want to check if ContactTableMetaData.XYZ is any of those then either do
String selection = ContactTableMetaData.XYZ + "=? OR " + ContactTableMetaData.XYZ + "=? OR " + ...
or
String selection = ContactTableMetaData.XYZ + " IN (?, ?, ?, ?, ..)"
I would prefer the second one :)
回答2:
public static String getWhereStr(String columnName, int selectionArgsSize) {
String delim = ",";
final StringBuilder buf = new StringBuilder();
buf.append(columnName + " IN (");
for (int i = 0; i < selectionArgsSize; i++) {
if (i != 0) {
buf.append(delim);
}
buf.append("?");
}
buf.append(")");
return buf.toString();
}
来源:https://stackoverflow.com/questions/10304593/content-provider-error-bind-or-column-index-out-of-range-handle-0x234590