我正在寻找一种将 SQL 查询的结果/输出存储到数组中的方法。我有一个运行查询的 for 循环,每次运行查询时我想将结果存储在数组/数组列表中。我尝试使用游标,但无法在游标中存储多个字符串。
这是 for 循环:
for (int i=1;i<code.length;i++) {
Cursor cursor = myDataBase.query("codes", new String[]{"description"}, ("code = '" + code[i] + "'"), null, null, null, null);
cursor.moveToFirst();
String temp = cursor.getString(i);
result.add(i, temp);
cursor.close();
这似乎不起作用。
有什么建议或示例可以提供帮助吗?
谢谢
最佳答案
假设 code
是一个 ID 列表,表名称名为 codes
,并且您想要检索所有代码的描述列表尝试这样做(使用 Apache commons 中的 StringUtils.join
):
String codes = StringUtils.join(code,",");
Cursor cursor = myDataBase.rawQuery("select description from codes where code in (?)",new String[]{codes});
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result.add(cursor.getString(1));
}
cursor.close();
关于java - 将 SQL 查询输出存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34277345/