java - 获取数组列表来自 SQLite
问题描述
我将我的 ArrayList 保存在 SQLite 中。保存代码:
public void HistoryADD(ArrayList<String> full, String owner){
int size = full.size();
SQLiteDatabase db = getWritableDatabase();
try{
for (int i = 0; i < size ; i++){
ContentValues cv = new ContentValues();
cv.put(FULL, full.get(i));
cv.put(OWNER, owner);
db.insert(TABLE_NAME, null, cv);
}
System.out.println("added:" + full);
db.close();
}catch (Exception e){
System.out.println("Failed to add" + full);
}
}
它正在工作。我想从 SQLite 获取这个 ArrayLists。清单代码:
public ArrayList<String> History_list(String owner) {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> history_list = new ArrayList<>();
Cursor cursor = db.rawQuery("SELECT * from " + TABLE_NAME + " WHERE owner='"+owner+"'",
new String[] {});
cursor.close();
return history_list;
}
但列表代码不起作用。这段代码有什么问题?谢谢。
解决方案
你需要做这样的事情:
Cursor cursor = db.rawQuery(...);
try {
while (cursor.moveToNext()) {
//Here you have to add the item in your array list
}
} finally {
cursor.close();
}
推荐阅读
- node.js - 为什么在 Windows 上通过 yarn 运行时,utf16 / 代理对表情符号无法在 console.log() 中正确显示?(直接npm和node都可以)
- sql-server - SQL Server 中的聚集索引:列在架构中的第一位有什么优势?
- android - Android布局是重叠元素
- wordpress - 如何从 Woocommerce 中的“我的帐户 > 订单”页面获取订单信息?$order->get_id() 与订单号不匹配
- angular - 引导轮播不工作在角度(10)
- javascript - 从嵌套数组中删除对象失败
- typeorm - 左连接和选择但不同
- javascript - 角材料不过滤选项
- elasticsearch - Elasticsearch - 使用字段匹配来提升而不是获取文档
- mongodb - 无法在 MongoDB GRAPHQL 查询中使用变量