我正在使用SQLite开发应用程序,每一行都有一列名称字符串和一列检查器。基本上,我数据库中的每个人都有一个姓名和一个检查器,该检查器显示该人是否已申请课程。
COLUMN_NAMES的字符串类似“ ALEX”,“ JASON”等,COLUMN_CHECK的字符串类似“ YES”或“ NO”。按一下按钮,我想生成一个字符串,例如“ ALEX,JASON,WALTER,TOM和HEISENBERG”,只带有COLUMN_CHECK值为“ YES”的名称。
如果我在上面没有理解自己,这里是一个图像示例:
我有一个名为CRUD.class的DatabaseHelper类,这些是我的类中的一些关键行:
Entries.class(数据类):
public static final String COLUMN_NAMES = "names";
public static final String COLUMN_CHECK = "check";
public static final String TABLE_NAME = "entries";
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUM_NAMES + " TEXT,"
+ COLUMN_CHECK + " TEXT"
+ ")";
public Entry() {
}
public Entry(int id, String names, String check) {
this.id = id;
this.names = names;
this.check = check;
}
public int getId() { return id; }
public void setId(int id){ this.id = id; }
public String getNames() { return names; }
public void setNames(String names){ this.names = names; }
public String getCheck() { return check; }
public void setCheck(String check){ this.check = check; }
MainActivity.class:
private EntryAdapter mAdapter;
private List<Entry> entryList = new ArrayList<>();
private RecyclerView recyclerView;
private CRUD db;
db = new CRUD(this);
entryList.addAll(db.getAllEntries());
mAdapter = new EntryAdapter(this, entryList);
RecyclerView.LayoutManager mLayoutManager = new
LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
提前谢谢!
答案 0 :(得分:0)
您可以在此处使用SQLite的GROUP_CONCAT
函数:
SELECT GROUP_CONCAT(names) AS names_csv
FROM entries
WHERE "check" = 'YES';
旁注:请避免为表名和列名使用check
之类的SQLite关键字。为了使上述查询有效,您必须用双引号将check
转义。