I have a database with a lot of tables. And one of these tables stores the name of some tables.

I would like to export just the tables that have their name in this table which store names.

For example: StoreTable stores the names A, B, C.

In my database, I have the tables A, B, D, E, F.

When I export, the method will export just the tables A and B, because their name are in the StoreTable.

I research a little, and notice that could be done with a select inside a select, but I am not familiar with SQLite yet.

The code I already did exports all tables from the database, it is bellowe:

@TargetApi(Build.VERSION_CODES.M)
    public void exportTableFiltered(SQLiteDatabase db, String tableName) {

        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

            try {
                //Query
                Cursor cursor = db.rawQuery("SELECT * FROM \"" + tableName + "\"", null);

                exportDir = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EXPORTED/");

                if (!exportDir.exists())
                {
                    exportDir.mkdirs();
                }

                File file = new File(exportDir, tableName +".csv");
                file.createNewFile();

                CSVWriter csvWriter = new CSVWriter(new FileWriter(file.getAbsoluteFile(), true));

                //Writes content into .CSV
                while(cursor.moveToNext()) {

                    //just columm 1
                    String arrStr[] = { cursor.getString(1)};
                    csvWriter.writeNext(arrStr);
                }

                //Close
                cursor.close();
                csvWriter.close();

            } catch (Exception e) {

                Toast.makeText(this, "Catch 04: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }

        }

        else {

            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

        }

        Toast.makeText(getApplicationContext(), "CSV FILE GENERATED!!", Toast.LENGTH_SHORT).show();

    }

Thanks!

0 Answers