2011-09-21 7 views
1

프로그램을 작성하려고합니다. 프로그램의 펑크 션 (funktions)에서 CSV 파일로부터 sqlite 데이터베이스로 데이터를 임포트 할 수 있습니다.csv에서 SQLite 채우기

나는이 코드를 요금제로 사용했지만 올바르게 작동하지 않습니다. 내 문제는 그것이 내 csv 파일의 마지막 줄만 데이터베이스에 저장한다는 것입니다. 나는 무엇을 놓치고 있습니까?

DbHelper.createContact(first, last, phone, mphone, room, initial); 

이 while 루프 내부 속하는 : 여기

코드를

package com.android.FAC; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.IOException; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 



public class Importcsv extends Activity 
{ 


DbAdapter DbHelper; 
String notification = "Concats imported!"; 
TextView tView; 

String first; 
String last; 
String phone; 
String mphone; 
String room; 
String initial; 

public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle);  
    setContentView(R.layout.importcsv); 
    DbHelper = new DbAdapter(this); 
    DbHelper.open();  
    tView = (TextView) findViewById(R.id.textView1);   
    Button b2 = (Button) findViewById(R.id.btnClick2); 
    Button b3 = (Button) findViewById(R.id.btnClick3); 
    Button b4 = (Button) findViewById(R.id.btnClick4); 



    b2.setOnClickListener(new TextView.OnClickListener() { 
     public void onClick(View v) {     
      try { 
       readfromcsv(); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
    }); 

    b3.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      Intent i = new Intent(Importcsv.this, MainActivity.class); 
      Importcsv.this.startActivity(i); 
    } 
    }); 

    b4.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 

      int n = 0; 
      while(n<30) 
      { 
      DbHelper.deleteContact(n); 
      n++; 
      } 
    } 
    }); 
} 

public void readfromcsv() throws IOException 
{ 

    File sdcard = Environment.getExternalStorageDirectory(); 
    File file = new File(sdcard,"csvtest.csv"); 

    BufferedReader in = new BufferedReader(new FileReader(file)); 
    String reader = ""; 
    while ((reader = in.readLine()) != null) 
    { 
     String[] RowData = reader.split(","); 

     first = RowData[0]; 
     last = RowData[1]; 
     phone = RowData[2]; 
     mphone = RowData[3]; 
     room = RowData[4]; 
     initial = RowData[5]; 



    } 

    DbHelper.createContact(first, last, phone, mphone, room, initial); 
    in.close(); 
    Context context = getApplicationContext(); 
    int duration = Toast.LENGTH_SHORT; 
    Toast toast = Toast.makeText(context, notification, duration); 
    toast.show(); 
} 
} 

감사 피터

답변

2

아마도이 줄을 수 있습니다. 다음과 같이

+0

그 트릭을 할 것입니다. :) – Squonk

0
은 while 루프를 변경

:

while ((reader = in.readLine()) != null) 
    { 
     String[] RowData = reader.split(","); 

     first = RowData[0]; 
     last = RowData[1]; 
     phone = RowData[2]; 
     mphone = RowData[3]; 
     room = RowData[4]; 
     initial = RowData[5]; 

     DbHelper.createContact(first, last, phone, mphone, room, initial); 

    } 
관련 문제