Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

android - Convert database .db file into .csv

I am developing an application in which I am using database and storing that database file with .db extention into sdcard .. Now I want to convert this "db file into .csv", so that the user can open that .csv file and can easily able to see all its data...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hey I got the answer of my own question: I downloaded a library OpenCSV and added opencsv.jar file into my application and the below code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...