我正在尝试从本地sqlite数据库将整个表导出到.csv文件,并使用opencsv库将其存储在sdcard中。
出口代码
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对象拉出并遍历它们。
谢谢你的时间。如果需要任何进一步的信息,请询问。
最佳答案
当我仍在寻找“更好”的解决方案时,我用它来解决我的问题。
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();
}
虽然我认为这不是最好的解决方案,但它非常简单易懂。如果有人提出更好的解决方案,我会很乐意接受他们的回答