1
我正在嘗試編寫一個程序。關於該程序的功能是能夠將數據從csv文件導入到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();
}
}
感謝彼得
這會做的伎倆。 :) – Squonk