2014-11-03 59 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

相關問題