Android在SQLite查询中不同

时间:2013-01-18 10:19:43

标签: android sqlite

return mDb.query(DATABASE_TABLE, new String[] {rfrom
}, null, null, null, null, null);

下面的查询是通过使用sqlite的rawquery标签的方式正确的语句。我想要的是我想使用现有的sqlite查询将以下查询语句替换为上述查询。无论如何可以完成吗?

select distinct col from 
(
    SELECT classa col FROM school 
    union all 
    SELECT classb col FROM school
) a 
order by col

1 个答案:

答案 0 :(得分:0)

您可以尝试使用SQLiteQueryBuilder

public void testUnionQuery() {
    String expected;
    String[] innerProjection = new String[] {"name", "age", "location"};
    SQLiteQueryBuilder employeeQueryBuilder = new SQLiteQueryBuilder();
    SQLiteQueryBuilder peopleQueryBuilder = new SQLiteQueryBuilder();
    employeeQueryBuilder.setTables("employee");
    peopleQueryBuilder.setTables("people");
    String employeeSubQuery = employeeQueryBuilder.buildUnionSubQuery(
            "_id", innerProjection,
            null, 2, "employee",
            "age=25",
            null, null, null);
    String peopleSubQuery = peopleQueryBuilder.buildUnionSubQuery(
            "_id", innerProjection,
            null, 2, "people",
            "location=LA",
            null, null, null);
    expected = "SELECT name, age, location FROM employee WHERE (age=25)";
    assertEquals(expected, employeeSubQuery);
    expected = "SELECT name, age, location FROM people WHERE (location=LA)";
    assertEquals(expected, peopleSubQuery);
    SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder();
    unionQueryBuilder.setDistinct(true);
    String unionQuery = unionQueryBuilder.buildUnionQuery(
            new String[] { employeeSubQuery, peopleSubQuery }, null, null);
    expected = "SELECT name, age, location FROM employee WHERE (age=25) " +
            "UNION SELECT name, age, location FROM people WHERE (location=LA)";
    assertEquals(expected, unionQuery);
}

有关详细信息,请访问Link