簡體   English   中英

導入多個.csv文件到android sqlite數據庫

[英]Import multiple .csv file into android sqlite database

我現在正在嘗試從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文件S的列不相同。(例如,有些可能有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
}

第二:使用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"因此請確保第一行上的列名以制表符分隔(顯然)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM