unnamet
unnamet

Reputation: 19

Populate SQLite from csv

I am trying to write a program. On of the funktions of the program is to be able to import data form a csv file to a sqlite database.

I have made this code so fare but it is not working properly. My problem is that it will only save the last line of my csv file in the database. What am i missing?

Here are the code

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

Thanks Peter

Upvotes: 1

Views: 2099

Answers (2)

Android Killer
Android Killer

Reputation: 18489

Change your while loop as follows:

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

    }

Upvotes: 0

Larry Lustig
Larry Lustig

Reputation: 50970

Presumably this line:

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

belongs inside the while loop.

Upvotes: 2

Related Questions