【问题标题】:Import multiple .csv file into android sqlite database将多个 .csv 文件导入 android sqlite 数据库
【发布时间】:2014-05-08 06:01:14
【问题描述】:

我现在正在尝试从 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,而另一种可能有4列名为C、D、E、F)除了hard为每个csv文件编码所有列,有什么可能的方法吗? 谁能告诉我任何解决方案???谢谢。

【问题讨论】:

  • 我在您添加编辑时发布了我的答案,所以我没有发现它。所以你的问题是列名不同吗? csv 的第一行是否包含列名?

标签: android sqlite csv import


【解决方案1】:

我能想到两种可能...

首先:如果您可以控制文件名,那么给它们命名时带有顺序数字方面,例如 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
}

第二种:使用listFiles()方法获取目录下的所有文件。示例...

// 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 文件使用第一行来描述列名。在某些方面,这有点像数据集的模式。示例(使用逗号分隔值)...

A,B,C,D
valueA,valueB,valueC,valueD
...

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

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

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

【讨论】:

  • 谢谢你,Squonk。但是如果不同的csv文件的列不一样,我该如何用通用的代码或者动态的方法来处理文件???
  • @Helloheyyyy :当您添加您的编辑时,我正在发布我的答案,所以我没有发现它。 csv 的第一行是否包含列名?
  • 不,csv的第一行没有列名
  • 您可以控制创建 csv 文件吗?
  • 否,csv 文件名不能更改。用 csv 名称对所有插入命令进行硬编码是实现我的目标的唯一方法吗???
猜你喜欢
  • 2016-01-21
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2014-08-18
  • 2015-09-30
  • 1970-01-01
相关资源
最近更新 更多