Export sqlite to csv

im trying to take data from my sqlite database and write it to a csv file and send email information when the recording is complete. I have only one table with 3 columns in the database. I have a DBAdapter and a class that has a button, the user must click to be able to export data.

Here is the code I tried Exportdata.java

try {
    root = Environment.getExternalStorageDirectory();                   
    Log.i(TAG,"path.." +root.getAbsolutePath());  

    //check sdcard permission  
    if (root.canWrite()) {  
        File fileDir = new File(root.getAbsolutePath()+"/fun/");  
        fileDir.mkdirs();  

        //   Log.d("DATABASE", db.getAllBYname());

        File file= new File(fileDir, "itisfun.csv");  
        FileWriter filewriter = new FileWriter(file);  
        BufferedWriter out = new BufferedWriter(filewriter);  
        out.write("I m enjoying......dude..............."  ); 
        out.close();  
    }  
} catch (IOException e) {  
    Log.e("ERROR:---", "Could not write file to SDCard" + e.getMessage());  
} 

This code creates the file in the SD card, but the problem is that I can’t get it to use the sqlite database data and write it to the file as shown in the code.im image, please help me with an example or edit my code with right decision.

+5
source share
1 answer

SQLite SD- CSV:

private Boolean backupDatabaseCSV(String outFileName) {
    MyLog.d(TAG, "backupDatabaseCSV");
    Boolean returnCode = false;
    int i = 0;
    String csvHeader = "";
    String csvValues = "";
    for (i = 0; i < GC.CURCOND_COLUMN_NAMES.length; i++) {
        if (csvHeader.length() > 0) {
            csvHeader += ",";
        }
        csvHeader += "\"" + GC.CURCOND_COLUMN_NAMES[i] + "\"";
    }

    csvHeader += "\n";
    MyLog.d(TAG, "header=" + csvHeader);
    dbAdapter.open();
    try {
        File outFile = new File(outFileName);
        FileWriter fileWriter = new FileWriter(outFile);
        BufferedWriter out = new BufferedWriter(fileWriter);
        Cursor cursor = dbAdapter.getAllRows();
        if (cursor != null) {
            out.write(csvHeader);
            while (cursor.moveToNext()) {
                csvValues = Long.toString(cursor.getLong(0)) + ",";
                csvValues += Double.toString(cursor.getDouble(1))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(2))
                        + ",";
                csvValues += "\"" + cursor.getString(3) + "\",";
                csvValues += Double.toString(cursor.getDouble(4))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(5))
                        + ",";
                csvValues += "\"" + cursor.getString(6) + "\",";
                csvValues += Double.toString(cursor.getDouble(7))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(8))
                        + ",";
                csvValues += Double.toString(cursor.getDouble(9))
                        + "\n";
                out.write(csvValues);
            }
            cursor.close();
        }
        out.close();
        returnCode = true;
    } catch (IOException e) {
        returnCode = false;
        MyLog.d(TAG, "IOException: " + e.getMessage());
    }
    dbAdapter.close();
    return returnCode;
}

GC - Global Constants, , , . CSV. GetAllRows . , . , . MyLog.d Log.d . DbAdapter :

DatabaseAdapter dbAdapter = null;

onCreate :

dbAdapter = new DatabaseAdapter(getApplicationContext());

dbAdapter . , , dbAdapter.open() dbAdapter.close() , . , , .

. csvValues ​​ , SQLite. , SQLite, csvValues ​​ CSV.

+10

All Articles