CSV文件编写问题android sqlite数据库

时间:2017-06-02 10:53:54

标签: android sqlite csv

我正在开发一个应用程序,我需要使用不同的ID从sqlite数据库中检索数据,我做了很多工作但我的问题是当我制作CSV文件时我也得到列名称以及每个id 就像我得到这种类型的CSV文件

please open this link to see picture

我正在使用此代码

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

}

}

请帮帮我

1 个答案:

答案 0 :(得分:0)

因为每次找到新的id时都会写入列名。

我在这里假设您可能想要动态获取列名,因此我获取了一个虚拟条目。

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

试试这样。

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