如何在 android 房间中使用 distinct
how to use distinct in android room
我正在使用 android room 而不是 sqllite。在我的应用程序数据库列中有多个记录。例如我的列名是国家。值为
- 印度
- 德国
- 卡塔尔
- 德国
- 爱尔兰
- 沙特
- 美国
- 德国
- 非洲
- 卡塔尔
- 德国
我要的结果是
- 印度
- 德国
- 卡塔尔
- 爱尔兰
- 沙特
- 美国
- 非洲
卡塔尔和德国来了不止一次。我想整理成单个项目。我用过SELECT DISTINCT name from country
。但它显示错误。我的代码如下所示。
CountryDao.java
@Dao
public interface CountryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void addCountry(Country country);
@Query("SELECT DISTINCT name from country")
public List<Country> getCountry();
}
Country.java
@Entity
public class Section {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
public Section(int id, String name){
this.setId(id);
this.setName(name);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
显示错误是
查询返回的列没有 com.android.db.Country 中的字段 [id],即使它们被注释为非空或原始。查询返回的列:[name]
AppDatabase.java
@Database(entities = {Country.class}, version = 5, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase{
private static AppDatabase INSTANCE;
public abstract ClassDao classDao();
public abstract SessionDao sessionDao();
public abstract SectionDao sectionDao();
public static AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context, AppDatabase.class, "edumiadatabase").fallbackToDestructiveMigration()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
在我的 activity 中通过添加此代码获取数据。 ``列表 co = database.countryDao().getCountry();
列表项 = database.countryDao().getCountry();
字符串列表
这里使用for循环进行迭代
我通过修改得到了答案
我已更改 CountryDao.java 中的查询。将查询重写为
@Query("SELECT DISTINCT name from country")
public List<String> getCountry();
然后像这样调用activity。
List<String> co = database.countryDao().getCountry();
for(int i=0; i<co.size();i++) {
Log.d(TAG, "country:" + co.get(i));
}
问题是我的代码是按对象获取列表的,但我希望它是字符串形式的。感谢所有的朋友。
我正在使用 android room 而不是 sqllite。在我的应用程序数据库列中有多个记录。例如我的列名是国家。值为
- 印度
- 德国
- 卡塔尔
- 德国
- 爱尔兰
- 沙特
- 美国
- 德国
- 非洲
- 卡塔尔
- 德国
我要的结果是
- 印度
- 德国
- 卡塔尔
- 爱尔兰
- 沙特
- 美国
- 非洲
卡塔尔和德国来了不止一次。我想整理成单个项目。我用过SELECT DISTINCT name from country
。但它显示错误。我的代码如下所示。
CountryDao.java
@Dao
public interface CountryDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void addCountry(Country country);
@Query("SELECT DISTINCT name from country")
public List<Country> getCountry();
}
Country.java
@Entity
public class Section {
@PrimaryKey(autoGenerate = true)
private int id;
private String name;
public Section(int id, String name){
this.setId(id);
this.setName(name);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
显示错误是 查询返回的列没有 com.android.db.Country 中的字段 [id],即使它们被注释为非空或原始。查询返回的列:[name]
AppDatabase.java
@Database(entities = {Country.class}, version = 5, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase{
private static AppDatabase INSTANCE;
public abstract ClassDao classDao();
public abstract SessionDao sessionDao();
public abstract SectionDao sectionDao();
public static AppDatabase getDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context, AppDatabase.class, "edumiadatabase").fallbackToDestructiveMigration()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
在我的 activity 中通过添加此代码获取数据。 ``列表 co = database.countryDao().getCountry();
列表项 = database.countryDao().getCountry(); 字符串列表 这里使用for循环进行迭代
我通过修改得到了答案 我已更改 CountryDao.java 中的查询。将查询重写为
@Query("SELECT DISTINCT name from country")
public List<String> getCountry();
然后像这样调用activity。
List<String> co = database.countryDao().getCountry();
for(int i=0; i<co.size();i++) {
Log.d(TAG, "country:" + co.get(i));
}
问题是我的代码是按对象获取列表的,但我希望它是字符串形式的。感谢所有的朋友。