gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:42:04 25 4
gpt4 key购买 nike

在进行更复杂的 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;
}

最佳答案

而不是...

public Cursor query(String table, String[] columns, String selection,
String[] selectionArgs, String groupBy, String having,
String orderBy)

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

public Cursor query (boolean distinct, String table, String[] columns, 
String selection, String[] selectionArgs, String groupBy,
String having, String orderBy, String limit)

...重载并将 distinct 设置为 true

Android 文档似乎有点难以直接链接,但描述两者的文档页面是 here .

关于android - 在 Android 中使用 SQLite 将 DISTINCT 关键字添加到 query(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17807115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com