简体   繁体   中英

How to export multiples tables in different CSV files?

I have a database with many tables. I would like to export all tables into different .csv files. For example, table 1 will generate 1.csv file. Table 2, 2.csv file. Etc.

This method works fine but I only export all tables into a single .csv file.

public void exportData (View view) {

    File dbFile = getDatabasePath("myDataBase.db");
    Helper dbhelper = new Helper(getApplicationContext());
    File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    if (!exportDir.exists())
    {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, tableName);
    try
    {
        file.createNewFile();
        CSVWriter csvWriter = new CSVWriter(new FileWriter(file));
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor curCSV = db.rawQuery("SELECT * FROM " + tableName,null);
        csvWriter.writeNext(curCSV.getColumnNames());

        while(curCSV.moveToNext())
        {
            String arrStr[] ={curCSV.getString(0),curCSV.getString(1)};
            csvWriter.writeNext(arrStr);
        }

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

    Toast.makeText(this, "File created!", Toast.LENGTH_SHORT).show();

}

I would like to export all tables into different .csv files, one for each table, with the table names + .csv extension.

Would you teach me how to do it?

I would encourage you to figure it out by your own, I will give you hint to point in right direction.

You need to identify for which table you need to export data and based on that loop through only those records which are in that table.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM