简体   繁体   English

如何在不同的CSV文件中导出多个表格?

[英]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. 我想将所有表导出到不同的.csv文件中。 For example, table 1 will generate 1.csv file. 例如,表1将生成1.csv文件。 Table 2, 2.csv file. 表2. 2.csv文件。 Etc. 等等。

This method works fine but I only export all tables into a single .csv file. 此方法工作正常,但我只将所有表导出到单个.csv文件中。

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. 我想将所有表导出到不同的.csv文件中,每个表一个,表名+ .csv扩展名。

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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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