mercredi 14 octobre 2015

accept comma in in csv file and import to SQLite

//iam developing apps that will import and export csv file . my problem is when i import csv file with comma it will not read the comma.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null)
        return;
    switch (requestCode) {
        case requestcode:
            String filepath = data.getData().getPath();
            controller = new DBController(getApplicationContext(), null, null, 1);
            SQLiteDatabase db = controller.getWritableDatabase();
            String tableName = "tbl_equipments";
            db.execSQL("delete from " + tableName);
            try {
                if (resultCode == RESULT_OK) {
                    try {
                        FileReader file = new FileReader(filepath);
                        BufferedReader buffer = new BufferedReader(file);
                        ContentValues contentValues = new ContentValues();
                        String line = "";
                        db.beginTransaction();
                        while ((line = buffer.readLine()) != null) {
                            String[] str = line.split(",", 6);  // defining 3 columns with null or blank field //values acceptance

                            //Id, Company,Name,Price
                            String equipment_id = str[0].toString();
                            String desc = str[1].toString();
                            String details = str[2].toString();
                            String resp = str[3].toString();


                            contentValues.put("equipment_id", equipment_id);
                            contentValues.put("description", desc);
                            contentValues.put("details", details);
                            contentValues.put("responsibility_center",resp);


                            db.insert(tableName, null, contentValues);
                            lbl.setText("Successfully Updated Database.");
                        }
                        db.setTransactionSuccessful();
                        db.endTransaction();
                    } catch (IOException e) {
                        if (db.inTransaction())
                            db.endTransaction();
                        Dialog d = new Dialog(this);
                        d.setTitle(e.getMessage().toString() + "first");
                        d.show();
                        // db.endTransaction();
                    }
                } else {
                    if (db.inTransaction())
                        db.endTransaction();
                    Dialog d = new Dialog(this);
                    d.setTitle("Only CSV files allowed");
                    d.show();
                }
            } catch (Exception ex) {
                if (db.inTransaction())
                    db.endTransaction();
                Dialog d = new Dialog(this);
                d.setTitle(ex.getMessage().toString() + "second");
                d.show();
                // db.endTransaction();
            }
    }
    myList= controller.getAllEquipments();
    if (myList.size() != 0) {
        ListView lv = getListView();
        ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
                R.layout.v, new String[]{"equipment_id", "description", "details"}, new int[]{
                R.id.txtequipment_id, R.id.txtdesc, R.id.txtdetails});
        setListAdapter(adapter);
        lbl.setText("Data Successfuly Imported");
    }
}

this is my code import csv file. but it will not accept comma . can someone teach how or edit my code. .

Aucun commentaire:

Enregistrer un commentaire