Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多个.csv文件导入android sqlite数据库_Android_Sqlite_Csv_Import - Fatal编程技术网

将多个.csv文件导入android sqlite数据库

将多个.csv文件导入android sqlite数据库,android,sqlite,csv,import,Android,Sqlite,Csv,Import,我现在正试图从android设备的sd卡中的某个目录导入csv文件。最近,我可以成功导入单个csv文件。但是,我不知道如何获取所有csv文件的列表,然后使用循环逐个导入csv文件 这是导入单个csv的我的代码: button_import_csv.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ DatabaseHelper helper = new

我现在正试图从android设备的sd卡中的某个目录导入csv文件。最近,我可以成功导入单个csv文件。但是,我不知道如何获取所有csv文件的列表,然后使用循环逐个导入csv文件

这是导入单个csv的我的代码:

button_import_csv.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){


            DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
            SQLiteDatabase db = helper.getWritableDatabase();


            try{
                FileReader file = new FileReader("/sdcard/downloadedfolder/A1/adv_sales_order.csv");
                BufferedReader buffer = new BufferedReader(file);
                ContentValues contentValues=new ContentValues();
                String line = "";
                String tableName ="adv_sales_order";

                db.beginTransaction();
                while ((line = buffer.readLine()) != null) {
                    String[] str = line.split("\t");


                    contentValues.put("order_date", str[0]);
                    contentValues.put("cust_code", str[1]);
                    contentValues.put("customer_ref_no", str[2]);
                    contentValues.put("line_no", str[3]);
                    contentValues.put("item_code", str[4]);
                    contentValues.put("tran_code", str[5]);
                    contentValues.put("order_qty", str[6]);
                    db.insert(tableName, null, contentValues);

                }
                db.setTransactionSuccessful();
                db.endTransaction();
            }catch (IOException e){

            }
        }
    });
不同csv文件的列不相同(例如,有些可能有4列,分别命名为A、B、C、D,另一列可能有列,分别命名为C、D、E、F)。除了对每个csv文件的所有列进行硬编码外,还有什么可能的方法吗?
谁能告诉我解决办法吗???谢谢

我可以想到两种可能性

首先:如果您控制文件名,则为其指定具有顺序数字特征的名称,例如file1.csv、file2.csv等,然后您可以简单地使用
for
循环来构建文件名并对其进行处理。例如

// Lets say you have 5 files named file1.csv thru file5.csv
for(int i = 1; i < 6; i++) {
    String filename = "file" + i + ".csv";
    // Process the file which has the above filename
}
// This code assumes you have a File object for the directory called dir
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++) {
    String filename = files[i].getAbsolutePath();
    if (filename.endsWith(".csv")) {
        // Process the file which has the above filename
    }
}
我不确定上面的两个代码块是否完美,但基本上它们都只是使用
for
循环。还有其他方法,但这些都是最直接的方法

编辑: 一些csv文件使用第一行来描述列名。在某些方面,这有点像数据集的模式。示例(使用逗号分隔的值)

使用这种方法意味着您可以通过读取第一行并将其拆分成数组来访问列名。然后可以使用
for
循环来放置contentvalue。试试下面的

// Read the first line separately and split to get the column names
line = buffer.readLine();
String[] cols = line.split("\t");
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
    String[] str = line.split("\t");

    for (int i = 0; i < cols.length; i++) {
        contentValues.put(cols[i], str[i]);
    }
    db.insert(tableName, null, contentValues);
}
db.setTransactionSuccessful();
db.endTransaction();
//分别读取第一行并拆分以获取列名
line=buffer.readLine();
字符串[]cols=line.split(“\t”);
db.beginTransaction();
而((line=buffer.readLine())!=null){
字符串[]str=line.split(“\t”);
for(int i=0;i

顺便说一句,我注意到您正在拆分
“\t”
,因此请确保第一行的列名以制表符分隔(显然)。

谢谢您,Squonk。但是,如果不同csv文件的列不同,我如何使用常规代码或动态方法处理该文件???@Helloheyyyy:当您添加编辑内容时,我正在发布我的答案,所以我没有发现它。csv的第一行是否包含列名?否,csv的第一行中没有列名。您可以控制创建csv文件吗?否,csv文件的名称无法更改。用csv名称硬编码所有插入命令是实现我目标的唯一方法吗???当你添加编辑时,我正在发布我的答案,所以我没有发现它。所以你的问题是列名不同,对吗?csv的第一行是否包含列名?
// Read the first line separately and split to get the column names
line = buffer.readLine();
String[] cols = line.split("\t");
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
    String[] str = line.split("\t");

    for (int i = 0; i < cols.length; i++) {
        contentValues.put(cols[i], str[i]);
    }
    db.insert(tableName, null, contentValues);
}
db.setTransactionSuccessful();
db.endTransaction();