android - SQLite 如何将列的所有字符串内容添加到单个字符串(带条件)
问题描述
我正在使用 SQLite 开发一个应用程序,每一行都有一列名称字符串和一列检查器。基本上,我数据库中的每个人都有一个姓名和一个检查器,显示该人是否已申请课程。COLUMN_NAMES 有“ALEX”、“JASON”等字符串,COLUMN_CHECK 有“YES”或“NO”等字符串。
按一下按钮,我想生成一个像“ALEX、JASON、WALTER、TOM、HEISENBERG”这样的字符串,其中只有那些具有 COLUMN_CHECK 值“YES”的名称。
如果我没有让自己在上面理解,这是一个图像示例:
我有一个名为 CRUD.class 的 DatabaseHelper 类,这些是我的类中的一些关键行:
Entry.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);
先感谢您!
解决方案
你可以在这里使用 SQLite 的GROUP_CONCAT
函数:
SELECT GROUP_CONCAT(names) AS names_csv
FROM entries
WHERE "check" = 'YES';
演示
旁注:请避免在表和列名称中使用 SQLite 关键字check
。要使上述查询正常工作,您必须check
用双引号转义。
推荐阅读
- python - 无法将文件从 DBFS 复制到 Databricks 中的本地桌面
- python - 使用 groupby 命令从数据框列表中堆叠条形图
- pandas - 生成总和为 1 的值列表 - 在 groupby 内?
- sql - Oracle 存储过程未按预期加载数据
- python - Python pandas根据日期和列添加小时修改数据框
- regex - 写入数据时公式不会停止跳行
- ruby-on-rails - vscode扩展颜色设置会覆盖settings.json颜色设置,如何解决?
- node.js - 从 React js 应用程序读取快速会话 cookie
- amazon-s3 - 从 Apex 调用 S3 时 AWS 访问密钥无效
- octave - 放大 Octave rlocus 上的标记