vendredi 13 mars 2015

My method to convert CSV to SQLite is taking a long time

My app has over 10,000 rows of data that it needs to reference. I've stored them in a CSV file, which I use to populate a SQLite database table. It works great, except it takes over 2 minutes to import. Could someone look at this code and possible suggest a quicker method?


By the way, I'm deploying with a CSV instead of a pre-compiled database file because I've read that SQLite files are not compatible across all devices and Android versions.


In my Main Activity, I call



CSVReader csvReader = null;

System.out.println(" exists is... " + input.toString());
try {
csvReader = new CSVReader(new InputStreamReader(input));
} catch (Exception e) {
e.printStackTrace();
}

DBAdapter db = new DBAdapter(context);

db.open();
db.restoreDBFromCSVReader(csvReader);
db.close();
...


Here is the restoreDBFromCSVReader() method in my DBAdapter class.



public boolean restoreDBFromCSVReader(CSVReader reader){
ContentValues vals = new ContentValues();

long success = 1;
int j;
int colNum;
String[] line;
String table = TABLE_CLUE;
String[] headers;
String[] nextLine;
try {
headers = reader.readNext(); // headers are the first line
while ((nextLine = reader.readNext()) != null) { // get rows until end of table
colNum = headers.length;

for (j = 0; j < colNum; j++) { // contentValues for this row
try {
if (!headers[j].equals(KEY_ID)) {//don't insert for _id
vals.put(headers[j], nextLine[j]);
}
} catch (Exception e) {
return false;
}
}
success = db.insert(table, null, vals);
if (success < 0) {
return false;
}
vals.clear();
}
} catch (IOException ioex){
ioex.printStackTrace();
}

if(success < 0)
return false;
return true;
}

Aucun commentaire:

Enregistrer un commentaire