gpt4 book ai didi

android - 从 csv 填充 SQLite

转载 作者:搜寻专家 更新时间:2023-10-30 23:20:18 24 4
gpt4 key购买 nike

我正在尝试编写一个程序。该程序的功能之一是能够将数据从 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();
}
}

谢谢彼得

最佳答案

大概是这一行:

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

属于 while 循环内部

关于android - 从 csv 填充 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7505259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com