mardi 14 avril 2015

Every time when app launches it reads data from csv and repeated data is going into sqlite database

I am trying to read data from csv file and inserting into sqlite database.But all data were not getting inserting into database. Suggest me suitable code for it


Main Activity



// reading csv file from raw folder

private void insertFromCsvToSqliteDB()` {

databaseHelper = new DatabaseHelper(this);
InputStream inputStream = getResources().openRawResource(
R.raw.appconfig);
// Log.i(TAG, "inputStream: " + inputStream);

List<String[]> resultList = new ArrayList<String[]>();
//Log.i(TAG, "resultList: " + resultList);
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
Log.i(TAG, "csvLine: " + csvLine);
row = csvLine.split(",");

resultList.add(row);

// loop for inserting values into table
databaseHelper.insertContact(row[0], row[1], row[2], row[3],
row[4], row[5], row[6]);
}
} catch (IOException e) {
e.printStackTrace();
}
}


Database Helper



public boolean insertContact(String[] rowData) {
// TODO Auto-generated method stub`

Log.i(TAG, "inside insertContact()");
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("DistrictCd", rowData[0]);
contentValues.put("ProspectId", rowData[1]);
contentValues.put("categorydesc",rowData[2]);
contentValues.put("BusinessName", rowData[3]);
contentValues.put("ph_Phone", rowData[4]);
contentValues.put("LandMark", rowData[5]);
contentValues.put("CategoryId", rowData[6]);
db.insert("Category", null, contentValues);

Log.i(TAG, "data inserted succesfully into db");
return true;

}


Let me suggestions if any for resolving this issue


Aucun commentaire:

Enregistrer un commentaire