2013-01-18 71 views
0
return mDb.query(DATABASE_TABLE, new String[] {rfrom 
}, null, null, null, null, null); 

下面的查询是正确的语句与sqlite的rawquery标签一起使用的方式。我想要的是我想用上面的查询替换下面的查询语句使用现有的sqlite的查询。无论如何可以完成吗?Android独特的SQLite查询

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

'query'不支持子查询。为什么不使用['SQLiteQueryBuilder'](http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html)? –

+0

只有使用SQLiteQueryBuilder才有可能。你想以这种方式实施吗? – GrIsHu

+0

yap,你能让我知道如何使用SQLiteQueryBuilder进行上面的sql语句吗? – ppshein

回答

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