I tried out the method mentioned here. Soon I found out that this was not the thing that I needed .
I needed the table level data which had the transactional information and not database level data.
So CSV was a good option.
In Android phones you can view the csv file in an excel format using ThinkFree Office tools. Similarly in PC using MSOffice Excel.
The code structure is similar to the export to DB as used in the previous post, the difference is that we use a thrid-party Java CSV library called OPENCSV.
/***************************************************************************************************************************************************************Export Database into CSV Class***************************************************************************************************************************************************************/class ExportDatabaseCSVTask extends AsyncTask<String, Void, Boolean>{private final ProgressDialog dialog = new ProgressDialog(ctx);// can use UI thread here@Overrideprotected 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("excerDB.db");DbClass DBob = new DbClass(MainActivity.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 = db.rawQuery("SELECT * FROM ExcerTable",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@Overrideprotected void onPostExecute(final Boolean success){if (this.dialog.isShowing()){this.dialog.dismiss();}if (success){Toast.makeText(ctx, "Export successful!", Toast.LENGTH_SHORT).show();}else{Toast.makeText(ctx, "Export failed", Toast.LENGTH_SHORT).show();}}}
This is used with a button Listener as follows -
Context ctx = MainActivity.this;//Create a Button listener for the CSV ExportButton impCSVBtn = (Button) findViewById(R.id.imp_csv_id);impCSVBtn.setOnClickListener(new OnClickListener() {public void onClick(View view){try{new ExportDatabaseCSVTask().execute("");}catch(Exception ex){Log.e("Error in MainActivity",ex.toString());}}});
This solved the export data problem for my app.
References :
http://stackoverflow.com ( No specific question but after reading the responses for similar questions I came up with this ).
Related Reading :
Finally after countless hours of research I was able to export my tables thanks to your tutorials!!!
Thanks! This tutorial was awesome! I’d suggest adding a link for the opencsv .jar file as well
Now I have to move on to importing from csv…
@rahul or anyone else :can you help me out in using DbClass???..its urgent..