0

i am developing a app in which i need to retrieve data from sqlite database using different ids, i have done much work but my problem is when i make CSV file i get also columns names as well with every id Like i am get CSV file in this type

please open this link to see picture

I am using this code

    public void getData() {


    File dbFile = getDatabasePath("formantime.db");
    SQLITEDBTABLE dbhelper = new SQLITEDBTABLE(getApplicationContext());
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");
    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }

    file = new File(exportDir, "FTSData.csv");

    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
        SQLiteDatabase db = dbhelper.getReadableDatabase();

        for (String ids : TimeEntryListViewAdapter.emailintArrayList) {


            curCSV = db.rawQuery("SELECT * FROM RecordsTable WHERE RecordId ='" + ids + "'", null);
            csvWrite.writeNext(curCSV.getColumnNames());
            if (curCSV.moveToFirst()) {
                do {
                    //Which column you want to exprort

                    String[] arrStr = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2), curCSV.getString(3),
                            curCSV.getString(4), curCSV.getString(5), curCSV.getString(6)};
                    csvWrite.writeNext(arrStr);

                } while (curCSV.moveToNext());


            }
            curCSV.close();
        }

        csvWrite.close();
    } catch (Exception sqlEx) {
        Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
    }


    Uri u1 = null;
    u1 = Uri.fromFile(file);


    String[] emails;
    ArrayList<String> mails = EmailReportsAdapter.emailsList;
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType("text/richtext");
    sendIntent.putExtra(Intent.EXTRA_EMAIL, mails.toArray(new String[mails.size()]));
    sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
    try {
        startActivity(Intent.createChooser(sendIntent, "Sending mail.."));

    } catch (android.content.ActivityNotFoundException ex) {

        Toast.makeText(EmailReports.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();

    }
    TimeEntryListViewAdapter.emailintArrayList.clear();

}

}

Please help me

1 Answer 1

1

its because you write the column names each time a new id was found.

I assumed here you might want to fetch columnnames dynamically so i fetched a dummy entry.

curCSV = db.rawQuery("SELECT * FROM RecordsTable WHERE RecordId ='1'", null);

Try like this.

try {
             file.createNewFile();
             CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
             SQLiteDatabase db = dbhelper.getReadableDatabase();

             /* fetch a dummy entry */
             curCSV = db.rawQuery("SELECT * FROM RecordsTable WHERE RecordId ='1'", null);

             csvWrite.writeNext(curCSV.getColumnNames()); // write column names only once.

             for (String ids : TimeEntryListViewAdapter.emailintArrayList) {

                curCSV = db.rawQuery("SELECT * FROM RecordsTable WHERE RecordId ='" + ids + "'", null);

                 if (curCSV.moveToFirst()) {
                     do {
                         //Which column you want to exprort

                         String[] arrStr = {curCSV.getString(0), curCSV.getString(1), curCSV.getString(2), curCSV.getString(3),
                                 curCSV.getString(4), curCSV.getString(5), curCSV.getString(6)};
                         csvWrite.writeNext(arrStr);

                     } while (curCSV.moveToNext());


                 }
                 curCSV.close();
             }

             csvWrite.close();
         } catch (Exception sqlEx) {
             Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
         }
Sign up to request clarification or add additional context in comments.

8 Comments

selecting single id data is not a problem but i want to get multiple data using ids that is why i am using loop here as you can see above the query
This method will print data against id 1 and then print data against ids which are coming from loop? if i am wrong please correct me thanks :)
By using curCSV = you assign or reassign value to this object. So by using it a second time you overwrite previous data
No by using this code it is not writing any data to file :(
no not any error when i get file for email using intent there is no data in the file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.