Reputation: 81
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();
}
Upvotes: 1
Views: 107
Reputation: 81
I post my solution based on @SweetD3v answer, just in case it is useful for someone else.
public void importPlayersTable() throws FileNotFoundException {
SQLiteDatabase db = this.getWritableDatabase();
String filepath = "/sdcard/playersTable.csv";
FileInputStream fs = new FileInputStream(filepath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(fs));
String line = "";
db.beginTransaction();
try {
while ((line = buffer.readLine()) != null) {
String[] colums = line.split(",");
ContentValues cv = new ContentValues();
//cv.put("ID_PLAYER", colums[0].trim());
cv.put("QUE_QUE", colums[1].trim());
cv.put("ANS_QUE", colums[2].trim());
db.insert(MI_TABLA_PLAYERS, null, cv);
}
} catch (IOException e) {
e.printStackTrace();
}
db.setTransactionSuccessful();
db.endTransaction();
}
I also had to add a try-catch in the class that calls the method:
try {
d.importPlayersTable();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 3307
You can import .csv file with FileReader and then save it to SQLite. check this method.
private void csvToSQLite(String filePath){
FileInputStream fs = new FileInputStream(filePath);
BufferedReader buffer = new BufferedReader(new InputStreamReader(fs));
String line = "";
String tableName ="TABLE_NAME";
String columns = "_id, name, dt1, dt2, dt3";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
sb.append("'" + str[0] + "',");
sb.append(str[1] + "',");
sb.append(str[2] + "',");
sb.append(str[3] + "'");
sb.append(str[4] + "'");
sb.append(str2);
db.execSQL(sb.toString());
}
db.setTransactionSuccessful();
db.endTransaction();
}
Upvotes: 1