2012-11-25 86 views
0

我想將應用程序的SQLite數據庫導出爲CSV文件並存儲在SD卡中。導出方法在DBAdapter類中定義爲ExportToCSV。我檢查應用程序是否成功創建SQLite數據庫。但是,當我嘗試使用MainActivity中的onPause方法調用ExportToCSV時,應用程序無法響應導出數據庫。你能給我一些關於如何改正這段代碼的意見嗎?我會感謝您的幫助!評論需要將android Sqlite數據庫導出爲CSV

我的代碼如下:

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(); 
       } 
      } 

我用的onPause在MainActivity.java調用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); 
    } 
+0

你是什麼意思「無法響應出口」?是否有任何函數*未被調用?你有例外嗎? –

+0

雖然應用程序不斷生成數據,但似乎ExportToCSV無法運行。我在HTC狀態下運行該應用程序,它顯示「Acitivity MainActivity(在應用程序數據庫中)沒有響應。」 –

回答

0

您必須在最後關閉文件。

bfw.close();