开发者

Can you query two tables in one statement in Android?

I'm trying to reduce the number of queries I do to the Android's database. Basically I want to get any email or IM address that contains a user defined search term. Right now I'm doing two separate queries:

Email:

Cursor emails = cr.query( 
    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
    null, 
    ContactsContract.CommonDataKinds.Email.DATA + " LIKE ?", 
    new String[] {searchTerm}, 
    null);

IM:

Cursor cursor = cr.query(
    ContactsContract.Data.CONTENT_URI, 
    null, 
    ContactsContract.CommonDataKinds.Im.DATA + " LIKE ?", 
    new String[] {searchTerm}, 
    null); 
开发者_StackOverflow社区

Does anyone know of an easy way to combine both queries? The crux of the problem seems to be that the two content URI's are different.

Simon


You could try joining the tables using a MatrixCursor.

Here is an example.


This solution is easier: don't use a ContentResolver. Use SQLiteDatabase db = bdd.getReadableDatabase() (bdd is your SQLiteHelper) and then:

db.rawquery ("HERE YOU WRITE YOUR QUERY WITH TWO TABLES", "HERE SelectionArgs").

Example:

db.rawquery("SELECT a.*, b.id FROM TableA AS a, TableB AS b WHERE a.id=b.id", null);

or the same with another typing:

db.rawquery("SELECT a.*, b.id FROM TableA AS a, TableB AS b WHERE a.id=?", "b.id");

I hope this helps. ;-)


It is possible if you are developing your own content provider. You simple need to add new URI and execute custom query when you processing it.

However it is impossible if you are using 3rd party content provider because SQLite database (or something else) is encapsulated for it's consumer.


I found out that if you do a query like this:

Cursor c = mApp.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
    null,
    null,
    null,
    ContactsContract.Data.CONTACT_ID + " ASC");

You will get a very large table with all the data. Then you need to check the mimetype column to find out what type of data is contained in that row. From that you can extract all the emails and IMs for a given contact.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜