我正在尝试将应用程序的 SQLite db 导出到 CSV 文件并存储在 SD 卡中。导出方法在 DBAdapter 类中定义为 ExportToCSV。我已检查该应用程序是否成功创建了 SQLite 数据库。但是,当我尝试在 MainActivity 中使用 onPause 方法调用 ExportToCSV 时,应用程序无法响应导出数据库。您能否就如何更正此代码给我任何意见?我会感谢你的帮助!
我的代码如下:
public void ExportToCSV(Cursor c, String fileName) {
int rowCount = 0;
int colCount = 0;
FileWriter fw;
BufferedWriter bfw;
File sdCardDir = Environment.getExternalStorageDirectory();
File saveFile = new File(sdCardDir, fileName);
try {
rowCount = c.getCount();
colCount = c.getColumnCount();
fw = new FileWriter(saveFile);
bfw = new BufferedWriter(fw);
if (rowCount > 0) {
c.moveToFirst();
// write the colume title
for (int i = 0; i < colCount; i++) {
if (i != colCount - 1)
bfw.write(c.getColumnName(i) + ',');
else
bfw.write(c.getColumnName(i));
}
// change the line
bfw.newLine();
// write data
for (int i = 0; i < rowCount; i++) {
c.moveToPosition(i);
Log.v("exporting data", "exportting" + (i + 1) + "line");
for (int j = 0; j < colCount; j++) {
if (j != colCount - 1)
bfw.write(c.getString(j) + ',');
else
bfw.write(c.getString(j));
}
// change line
bfw.newLine();
}
}
我在 MainActivity.java 中使用 onPause 来调用 ExportToCSV:
@Override
protected void onPause() {
super.onPause();
DBAdapter myDatabase=new DBAdapter(this);
myDatabase.open();
Cursor c=myDatabase.getAllgpspoint();
// this method was defined in DBAdapter.java and returned a Cursor
myDatabase.ExportToCSV(c, "IRI.csv");
myDatabase.close();
mSensorManager.unregisterListener(this);
}