samedi 26 septembre 2015

How can i convert sqlite db into csv and then in to excel. im not using any dbhelper class.?

i had used this code for conversion: when i create the getReadableDatabase function i cannot return then SQLiteDatabaseobject.getReadableDatabase()

private void exportDBtoCSV() {

    File dbFile=getDatabasePath("/Database/"+SAMPLE_TABLE_NAME);
   //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));
         sqldb = this.getReadableDatabase();
        Cursor curCSV = sqldb.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);
    }
}
public SQLiteDatabase getReadableDatabase() {
    // TODO Auto-generated method stub
    return sqldb.getReadableDatabase();
}}

Aucun commentaire:

Enregistrer un commentaire