In my Android app, I am converting the SQLite .db
file to a .csv
file and saving on an external storage - this part has gone fine.
However, despite all the data being present, which is 23 columns,and when I put through a sample set of data, the output looked like:
"column_1_heading","column_2_heading","column_1_data","column_2_data",
where everything is in a single row and surrounded by quotation marks.
What I am after is for it to be in separate rows, without the quotation marks, such as:
column_1_heading,column_2_heading
column_1_data,column_2_data
They do not need to be lined up as columns, just in order in separate rows.
The code that I am using to perform the conversion is strongly based on this answer, the code of which is reproduced below:
File dbFile=getDatabasePath("MyDBName.db");
DBHelper dbhelper = new DBHelper(getApplicationContext());
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file = new File(exportDir, "csvname.csv");
try
{
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT * FROM contacts",null);
csvWrite.writeNext(curCSV.getColumnNames());
while(curCSV.moveToNext())
{
//Which column you want to exprort
String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
}
catch(Exception sqlEx)
{
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
}
Is there a change that needs to be made so that the output .csv file matches the desired output?