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.
While I'm still looking for a "better" solution I used this to solve my problem.
private void exportChartCsv() {
mDatabaseHelper = new DatabaseHelper(this);
try {
mWriter = new CSVWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/charts.csv"));
mChartList = mDatabaseHelper.getChartsList();
String[] mExportChartHeaders = {
"Chart ID",
"User ID",
"Patient ID",
"First Name",
"Last Name",
"Date of Birth",
"Telephone Number",
"Photo Path",
"TimeStamp",
"Questions Completed"
};
mWriter.writeNext(mExportChartHeaders);
for (ChartTable chartTable : mChartList) {
mId = String.valueOf(chartTable.getId());
mUserId = chartTable.getUserId();
mPatientId = chartTable.getPatientId();
mFirstName = chartTable.getFirstName();
mLastName = chartTable.getLastName();
mDateOfBirth = chartTable.getDob();
mTelephoneNumber = chartTable.getPhone();
mPhotoPath = chartTable.getPhoto();
mTimeStamp = chartTable.getTimeStamp();
mQuestionsCompleted = String.valueOf(chartTable.getQuestionsCompleted());
String[] mExportChart = {mId, mUserId, mPatientId, mFirstName, mLastName, mDateOfBirth, mTelephoneNumber, mPhotoPath, mTimeStamp, mQuestionsCompleted};
mWriter.writeNext(mExportChart);
}
mWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
mDatabaseHelper.close();
}
While I don't think this is the best solution it's very straight forward and easy to understand. If someone comes up with a better solution I will gladly accept their answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.