0

My objective is to write android sqlite database table into a csv file which can be read easily by human on a computer. I want to be able to open the csv file on my computer for viewing of data.

I have researched on stackoverflow about exporting database table to csv and one of code that i use is from Exporting SQLite Database to csv file in android. I have tried several other code on stackoverflow. All the other code suggested on stackoverflow is correct also but it cant seem to meet my objective.

Basically my code is similar to the link above.

My code can run without compilation error, meaning it can run without any error.

However, i faced a problem. Either the file was not created onto my computer for viewing or it is created in some android internal folder. I tried to search on window search button for ".csv". However i could not find the file that is supposed to be created. All i need is just a csv file with the database table data inside it.

I really appreciate if someone can guide me on what goes wrong or where i can locate the actual csv file.I am currently using an android phone. The following is my code that is of concern.

public void writeToCSV(){
        for (int i=21; i<37; i++){// write each average table to a csv file
            String tableNameString = "test" + i + "AveRssiTable";
            String csvFilename =tableNameString+".csv";
            File exportDir = new File(Environment.getExternalStorageDirectory(), "");
            if (!exportDir.exists())
            {
                exportDir.mkdirs();
            }

            File file = new File(exportDir, csvFilename);
            try
            {
                file.createNewFile();
                CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
                SQLiteDatabase rssiDB = this.openOrCreateDatabase("rssivalue", MODE_PRIVATE, null);
                Cursor curCSV = rssiDB.rawQuery("SELECT * FROM "+tableNameString,null);
                csvWrite.writeNext(curCSV.getColumnNames());
                while(curCSV.moveToNext())
                {
                    //Which column you want to export
                    String arrStr[] ={curCSV.getString(0)};
                    csvWrite.writeNext(arrStr);
                }
                csvWrite.close();
                curCSV.close();
                rssiDB.close();
            }
            catch(Exception sqlEx)
            {
                Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
            }
        }
    }
Community
  • 1
  • 1
xiaoxin
  • 7
  • 4

1 Answers1

0

If you are testing this code on emulator then your file is present in emulators SD card. You can access from Eclipse->DDMS->Devices(Select emulator)-File Explorer->storage->sdcard.

Click on the file then select a option of pull a file from device. save it then open it.

If you are testing your app on mobile then search it in your mobiles SD card.

If file is created successfully then you will find it in above locations. If file is not present in above location then there must be some error in your code

suvarna
  • 74
  • 3