我尝试将整个Table从本地sqlite数据库导出到.csv文件中,并使用opencsv库将其存储在我的SD卡上。
导出代码
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)
我很好理解错误,我无法将ArrayList<ChartTable>
转换为String[]
。此外,我的逻辑是有缺陷的。如您所见,ArrayList<ChartTable>
包含许多ChartTable对象,这些对象分别包含我希望在每个CSV行中输出的字符串。因此,如果我想使用此方法,我实际上需要从ArrayList<ChartTable>
获取每个ChartTable对象并将其转换为String []。现在我想,也许我试图将一个方形钉子放入圆孔中。
所以,鉴于这些信息,我尝试做什么的正确方法是什么?是否有更简单的方法来转储整个表?或者我应该简单地拉出每个ChartTable对象并迭代它们。
感谢您的时间。如果需要任何进一步的信息,请询问。
答案 0 :(得分:1)
虽然我还在寻找更好的&#34;解决方案我用它来解决我的问题。
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();
}
虽然我不认为这是最好的解决方案,但它非常直接且易于理解。如果有人提出更好的解决方案,我会很乐意接受他们的回答