5
我正在開發一個應用程序,我正在使用數據庫並將該數據庫文件與.db擴展存儲到sdcard中。現在我想將此「db文件轉換爲.csv」,如此用戶可以打開該.csv文件,並可以很容易地看到它的所有數據...將數據庫.db文件轉換爲.csv
我正在開發一個應用程序,我正在使用數據庫並將該數據庫文件與.db擴展存儲到sdcard中。現在我想將此「db文件轉換爲.csv」,如此用戶可以打開該.csv文件,並可以很容易地看到它的所有數據...將數據庫.db文件轉換爲.csv
嘿我得到了我自己的問題的答案:我下載了OpenCSV庫並將opencsv.jar文件添加到我的應用程序中,下面的代碼:
class ExportDatabaseCSVTask extends AsyncTask<String, Void, Boolean>{
private final ProgressDialog dialog = new ProgressDialog(MyDatabaseActivity.this);
// can use UI thread here
@Override
protected void onPreExecute(){
this.dialog.setMessage("Exporting database...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args){
File dbFile=getDatabasePath("mydb.db");
// DbClass DBob = new DbClass(MyDatabaseActivity.this);
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "excerDB.csv");
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
//SQLiteDatabase db = DBob.getReadableDatabase();
Cursor curCSV=mydb.rawQuery("select * from " + TableName_ans,null);
// Cursor curCSV = db.rawQuery("SELECT * FROM table_ans12",null);
csvWrite.writeNext(curCSV.getColumnNames());
while(curCSV.moveToNext()){
String arrStr[] ={curCSV.getString(0),curCSV.getString(1)};
/*curCSV.getString(2),curCSV.getString(3),curCSV.getString(4)*/
csvWrite.writeNext(arrStr);
}
csvWrite.close();
curCSV.close();
return true;
} catch(SQLException sqlEx) {
Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
return false;
} catch (IOException e) {
Log.e("MainActivity", e.getMessage(), e);
return false;
}
}
// can use UI thread here
@Override
protected void onPostExecute(final Boolean success) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
if (success) {
Toast.makeText(MyDatabaseActivity.this, "Export successful!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MyDatabaseActivity.this, "Export failed", Toast.LENGTH_SHORT).show();
}
}
}
怎麼可能把每個月的記錄放在新列中是.csv或excel。例如在'columnt A'中有一月份的記錄,而在'B欄'中記錄的是二月份的記錄..... – Pritesh 2014-05-25 14:11:04