2016-05-13 61 views

回答

0

嗨這段代碼可以幫助您:

private void exportDB() { 

     File dbFile=getDatabasePath("MyDBName.db"); 
     DBHelper dbhelper = new DBHelper(getApplicationContext()); 
     File exportDir = new File(Environment.getExternalStorageDirectory(), ""); 
     if (!exportDir.exists()) 
     { 
      exportDir.mkdirs(); 
     } 

     File file = new File(exportDir, "csvname.csv"); 
     try 
     { 
      file.createNewFile(); 
      CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); 
      SQLiteDatabase db = dbhelper.getReadableDatabase(); 
      Cursor curCSV = db.rawQuery("SELECT * FROM contacts",null); 
      csvWrite.writeNext(curCSV.getColumnNames()); 
      while(curCSV.moveToNext()) 
      { 
       //Which column you want to exprort 
       String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)}; 
       csvWrite.writeNext(arrStr); 
      } 
      csvWrite.close(); 
      curCSV.close(); 
     } 
     catch(Exception sqlEx) 
     { 
      Log.e("MainActivity", sqlEx.getMessage(), sqlEx); 
     } 

Exporting SQLite Database to csv file in android

How to convert data base records into csv file in android?

+0

對不起,是響應速度慢,我有很多的工作在那個問題之後,你的代碼支持了我很多,非常感謝。 關於 –

+0

如果這個答案有用,請投票;) –

相關問題