I have a method to import a CSV file from folder "assets" to an SQLite table.
The CSV contains lot of lines like this one: France;Paris
But I don´t want to take the CSV file from "assets" folder, I would like to modify that method to import a CSV file from the phone storage folder(/storage/emulated/0) but I don´t know what I have to change to achieve it. Can someone please help me? Thanks in advance.
This is the method that I want to modify:
public void importPlayersTable(Context context){
SQLiteDatabase db = this.getWritableDatabase();
AssetManager manager = context.getAssets();
InputStream inStream = null;
try {
inStream = manager.open("playersTable.csv");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(";");
ContentValues cv = new ContentValues();
cv.put("QUE_QUE", colums[0].trim());
cv.put("ANS_QUE", colums[1].trim());
db.insert(MI_TABLA_PLAYERS, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
2
Answers
I post my solution based on @SweetD3v answer, just in case it is useful for someone else.
I also had to add a try-catch in the class that calls the method:
You can import .csv file with FileReader and then save it to SQLite.
check this method.