我这里有这个 fragment :
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));
但是,即使我的文件中至少有两行数据,它也只保存文件中的第一行信息。我不知道为什么它不读取我的整个文件并将两行数据保存到数据库中?
最佳答案
将 insert()
移动到迭代集合的 for
循环中。
当前您仅插入循环最后一次迭代中的值。
关于java - 为什么读取文件时多行没有保存在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26720190/