我已实施内容提供商,导致以下错误
04-25 00:25:16.856:E / AndroidRuntime(520):引起:android.database.sqlite.SQLiteException:绑定或列索引超出范围:handle 0x234590
Content Provider中的查询是
Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null,
sortOrder);
并致电Content Provider
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);
完整的logcat是
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
感谢。
答案 0 :(得分:4)
String selection = ContactTableMetaData.XYZ + "=?";
String[] selectionArgs = new String[] { "phonenumber1", "phonenumber2", "phonenumber3",
"phonenumber4", "phonenumber5", "email1", "email2", "email3",
"email4", "email5" }
?
的金额必须与selectionArgs
的金额相匹配。如果您提供10个项目,那么selection
必须有10个?
。
?
中的第selection
个被selectionArgs
中的第N个项目替换。
编辑:如果你想检查ContactTableMetaData.XYZ是否是其中任何一个,那么
String selection = ContactTableMetaData.XYZ + "=? OR " + ContactTableMetaData.XYZ + "=? OR " + ...
或
String selection = ContactTableMetaData.XYZ + " IN (?, ?, ?, ?, ..)"
我更喜欢第二个:)
答案 1 :(得分:0)
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();
}