使用Android中的SQLite将DISTINCT关键字添加到query()

Kev*_*vik 2 sql database sqlite android

在进行更复杂的SQL查询时,SQLite中query()和rawQuery()之间的区别.

例如

我想使用SQL关键字DISTINCT,所以我没有从数据库中返回任何重复项.我理解如何使用rawQuery()方法,这样你就可以在方法中放入一个实际的SQL查询语句.通过这种方式,我可以使用rawQuery制作标准的SQL语句.使用rawQuery()时,很容易将DISTINCT关键字添加到任何SQL语句中

但是,当使用此代码中显示的query()方法时,我不能只使用常规SQL语句.在这种情况下,如何使用DISTINCT关键字作为查询的一部分进行查询?或具有相同功能的东西?

            // get info from country table
            public String[] getCountries(int numberOfRows) {

                     String[] columns = new String[]{COUNTRY_NAME};
                     String[] countries = new String[numberOfRows];

                     int counter = 0;

                Cursor cursor = sqLiteDatabase.query(COUNTRY_TABLE, columns, 
                            null, null, null, null, null);

                     if (cursor != null){

                     while(cursor.moveToNext()){
         countries[counter++] = cursor.getString(cursor.getColumnIndex(COUNTRY_NAME));                                   
                    }

            }
                return countries;                        
     }
Run Code Online (Sandbox Code Playgroud)

Joa*_*son 7

而不是......

public Cursor query(String table, String[] columns, String selection,
                    String[] selectionArgs, String groupBy, String having, 
                    String orderBy)
Run Code Online (Sandbox Code Playgroud)

...你正在使用的方法,只需使用......

public Cursor query (boolean distinct, String table, String[] columns, 
                     String selection, String[] selectionArgs, String groupBy,
                     String having, String orderBy, String limit)
Run Code Online (Sandbox Code Playgroud)

...过载并设置distincttrue.

Android文档似乎有点难以直接链接,但描述两者的文档页面就在这里.