samedi 8 août 2015

UNION in SQLiteDatabase?

I need to execute the following query, and return a Cursor, using SQLiteDatabase class:

SELECT DISTINCT sender
FROM messages;
UNION
SELECT DISTINCT recipient
FROM messages;

(the fields sender and recipient are of the same type.)

I know how to execute the SELECT DISTINCT and get 2 cursors holding sender and recipient columns:

c1 = db.query(
        true,                                   //distinct
        Messages.TABLE_NAME,                    //table name
        senderColumn,                           //columns
        null,                                   //where clause
        null,                                   //where args
        null,                                   //group
        null,                                   //having
        sortOrder,                              //sorting order
        null                                    //limit of rows number
);

c2 = db.query(
        true,                                   //distinct
        Messages.TABLE_NAME,                    //table name
        recipientColumn,                        //columns
        null,                                   //where clause
        null,                                   //where args
        null,                                   //group
        null,                                   //having
        sortOrder,                              //sorting order
        null                                    //limit of rows number
);

But I can't figure how to UNION them (and return a Cursor). Also, I may want to specify a sorting order on the result. How can I do that? I can't find any mention of UNION in the API.

Aucun commentaire:

Enregistrer un commentaire