我在这里有这个片段:
try {
Uri uri = Uri.parse("file:///storage/emulated/0/Download/information.csv");
File file = new File(uri.getPath());
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
boolean firstLine = true;
while ((line = br.readLine()) != null) {
System.out.println("Print the contents from the file :" + line);
if (firstLine) {
firstLine = false;
continue;
} else {
//deserialize the string to Object
TransferData tdBack = TransferData.fromString(line));
//Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
for (Collection collection : tdBack.getCollections()) {
values.put("reminders", collection.getReminders());
values.put("region", collection.getRegion().getId());
values.put("collection", collection.getCollection());
values.put("collection_interval", collection.getCollectionInterval().getId());
}
//insert the new row
sqLiteDatabase.insert(COLLECTION_TABLE, null, values);
Cursor cursor = sqLiteDatabase.rawQuery("Select * FROM collection", null);
Log.d("MainActivity", DatabaseUtils.dumpCursorToString(cursor));
但是,即使我的文件中至少有两行包含数据,它也只保存了我文件中的第一行信息。我不知道为什么它没有读取我的整个文件并将两行数据保存到数据库中?
答案 0 :(得分:1)
将insert()
移动到您迭代收藏集的for
循环内。
目前,您只需插入循环最后一次迭代的值。