I'm attempting to export an entire Table from my local sqlite database into a .csv file and store it on my sdcard using the opencsv library.
export code
private ArrayList<ChartTable> mChartList;
private String[] mExport;
private void exportCsv() {
mDatabaseHelper = new DatabaseHelper(this);
try {
mWriter = new CSVWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/charts.csv"));
mChartList = mDatabaseHelper.getChartsList();
mExport = mChartList.toArray(new String[mChartList.size()]);
mWriter.writeNext(mExport);
mWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
getChartsList()
public ArrayList<ChartTable> getChartsList() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<ChartTable> chartsList = new ArrayList<ChartTable>();
String[] sumColumns = {
ID_COL + ", "
+ USER_ID_COL + ", "
+ PATIENT_ID_COL + ", "
+ FIRST_NAME_COL + ", "
+ LAST_NAME_COL + ", "
+ DOB_COL + ", "
+ PHONE_COL + ", "
+ PHOTO_PATH_COL + ", "
+ TIME_STAMP_COL
};
Cursor c = db.query(true, CHART_TABLE, sumColumns, null, null, null, null, null, null);
if (c.moveToFirst()) {
do {
ChartTable ct = new ChartTable();
ct.setId(c.getInt(c.getColumnIndex(ID_COL)));
ct.setUserId((c.getString(c.getColumnIndex(USER_ID_COL))));
ct.setPatientId((c.getString(c.getColumnIndex(PATIENT_ID_COL))));
ct.setFirstName((c.getString(c.getColumnIndex(FIRST_NAME_COL))));
ct.setLastName(c.getString(c.getColumnIndex(LAST_NAME_COL)));
ct.setDob(c.getString(c.getColumnIndex(DOB_COL)));
ct.setPhone(c.getString(c.getColumnIndex(PHONE_COL)));
ct.setPhoto(c.getString(c.getColumnIndex(PHOTO_PATH_COL)));
ct.setTimeStamp(c.getString(c.getColumnIndex(TIME_STAMP_COL)));
chartsList.add(ct);
} while (c.moveToNext());
}
c.close();
db.close();
return chartsList;
}
LogCat
03-21 03:14:41.558: E/AndroidRuntime(1494): FATAL EXCEPTION: main
03-21 03:14:41.558: E/AndroidRuntime(1494): java.lang.ArrayStoreException: source[0] of type com.xxxxxx.models.ChartTable cannot be stored in destination array of type java.lang.String[]
03-21 03:14:41.558: E/AndroidRuntime(1494): at java.lang.System.arraycopy(Native Method)
03-21 03:14:41.558: E/AndroidRuntime(1494): at java.util.ArrayList.toArray(ArrayList.java:519)
03-21 03:14:41.558: E/AndroidRuntime(1494): at com.xxxxxx.activities.ImportExportActivity.exportCsv(ImportExportActivity.java:66)
03-21 03:14:41.558: E/AndroidRuntime(1494): at com.xxxxxx.activities.ImportExportActivity.access$1(ImportExportActivity.java:61)
03-21 03:14:41.558: E/AndroidRuntime(1494): at com.xxxxxx.activities.ImportExportActivity$2.onClick(ImportExportActivity.java:51)
03-21 03:14:41.558: E/AndroidRuntime(1494): at android.view.View.performClick(View.java:4204)
I understand the error well enough, I can't convert an ArrayList<ChartTable>
to a String[]
. Furthermore my logic was flawed. As you see, the ArrayList<ChartTable>
holds many ChartTable objects which individually hold the Strings I desire to output in each CSV row. So I would actually need to get each ChartTable object from my ArrayList<ChartTable>
and convert that to a String[] if I wanted to use this method. Now I'm thinking that maybe I'm trying to fit a square peg into a round hole.
So given that information, what is the correct way to do what I'm attempting? Is there an easier way to just dump the entire table? Or should I simply pull each ChartTable object out and iterate through them.
Thanks for your time. If any further information is needed please ask.