假设我有2个表,Contacts
和Phones
。用户可以输入Contacts
查询的名称。我用这个
Cursor cur = cr.query(
CONTACT_URI,
new String[] { CONTACT_COLUMN_ID, CONTACT_COLUMN_NAME },
CONTACT_COLLUMN_NAME + " LIKE '%" + search + "%'",
null,
null
);
然后我想从Phones
查询我从第一次查询得到的匹配联系人。我可以使用第三个参数来输入我的条件,例如“WHERE _ID = xx OR _ID = xx”等等......
我可以使用第一个查询/光标作为下一个查询的条件吗?我的第二个问题是这样的:
Cursor pCur = cr.query(
PHONE_URI,
new String[] { PHONE_COLUMN_ID, PHONE_COLUMN_DATA },
null,
null,
null
);
编辑:额外注意,我正在查询设备数据库,所以我只能使用Uri。
编辑2:或者,我可以使用此
Cursor pCur = cr.query(
PHONE_URI,
new String[] { PHONE_COLUMN_ID, PHONE_COLUMN_DATA },
PHONE_COLUMN_ID,
selectedIds,
null
);
但如果selectedIds太大,我得到了这个例外
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): FATAL EXCEPTION: main
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.laac.comp.phonelookup/com.laac.comp.phonelookup.PhoneContactLookup}: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x199d7c0
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.os.Looper.loop(Looper.java:143)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread.main(ActivityThread.java:4196)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at java.lang.reflect.Method.invoke(Method.java:507)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at dalvik.system.NativeStart.main(Native Method)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x199d7c0
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.content.ContentProviderProxy.query(ContentProviderNative.java:408)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.content.ContentResolver.query(ContentResolver.java:264)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at com.laac.comp.phonelookup.PhoneContactLookup.getContactWithPhone(PhoneContactLookup.java:255)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at com.laac.comp.phonelookup.PhoneContactLookup.onCreate(PhoneContactLookup.java:84)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
07-13 11:06:48.496: ERROR/AndroidRuntime(25137): ... 11 more
PhoneContactLookup.java:255
是我对EDIT 2的查询
答案 0 :(得分:0)
首先想到的是评论的一部分:
cr.rawQuery("SELECT * FROM " + CONTACT_URI + "as cu LEFT JOIN " + PHONE_URI + " as pu ON (cu.PHONE_COLUMN_ID = pu.PHONE_COLUMN_ID) WHERE CONTACT_COLUMN_NAME LIKE '%"+search+"%'");
第二个想法是基于你的编辑2我认为你需要先创建一个格式查询字符串吗?在选定的ID列表中填写的正确位置,所以我认为它会是这样的:
if(selectedIds.length==0)
return;
String formatString = "PHONE_COLUMN_ID=?";
for(int i=0;i<selectedIds.length-1;i++)
{
formatString+=" OR PHONE_COLUMN_ID=? ";
}
Cursor pCur = cr.query(
PHONE_URI,
new String[] { PHONE_COLUMN_ID, PHONE_COLUMN_DATA },
formatString,
selectedIds,
null
);