2014-11-03 45 views
0

我有这样的片段在这里:为什么读取文件时多行不会保存在数据库中?

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)); 

然而,即使我有至少两排,在我的文件数据,只保存的信息的第一行中我的文件。我不知道为什么它不读取我的整个文件并将两行数据保存到数据库中?

回答

1

insert()移动到您迭代集合的for循环中。

当前,您只是插入循环的最后一次迭代中的值。

+0

啊opps。谢谢! – 2014-11-03 18:13:14

相关问题