从csv填充SQLite

时间:2011-09-21 19:07:04

标签: android database sqlite

我正在尝试编写一个程序。关于该程序的功能是能够将数据从csv文件导入到sqlite数据库。

我已经使这个代码如此票价,但它无法正常工作。我的问题是它只会保存我的csv文件的最后一行在数据库中。我错过了什么?

以下是代码

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 :(得分:2)

大概这一行:

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

属于里面 while循环。

答案 1 :(得分: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);

    }