0

Doing research on how to export a database from SQLite, I already found the following code but was not able to make it work for me.

myDbAdapter.java:

public static void exportDB(Context context) {
    String databasePath = context.getDatabasePath(myDbHelper.DATABASE_NAME).getPath();
    String inFileName = databasePath;
    try {
        File dbFile = new File(inFileName);
        FileInputStream fis = new FileInputStream(dbFile);

        String outFileName = Environment.getExternalStorageDirectory() + "/" + myDbHelper.DATABASE_NAME;

        OutputStream output = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = fis.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
        //Close the streams
        output.flush();
        output.close();
        fis.close();
    } catch (Exception e) {
        Toast.makeText(context, "Export Failed", Toast.LENGTH_SHORT).show();
    }
}

MainActivity.java:

public void export(View view){
    helper.exportDB(getApplicationContext());
}

public static void main(String[] args){}

When I press the Export Button it says "Export Failed". Before I added the void main there was an error telling me that it is needed but I have no idea what to put into it. Additionally I don't think Environment.getExternalStorageDirectory() is the right way to find the directory since there is no SDCard in my Tablet. Any help appreciated!

Argos
  • 13
  • 5
  • I would not use a program to only export data from SQLite, because all decent databases can generate a csv file directly from their command line interface. `sqlite3` can explicitely: `.header on .mode csv .output filename.csv SELECT ...` – Serge Ballesta Feb 14 '18 at 11:09

1 Answers1

0

You can setup any destination for output file by yourself

p.s.This code should not export anything. It just rewrite the same data.

XemyL
  • 3
  • 2
  • Well that makes sense haha thanks. How do you export it then? – Argos Feb 14 '18 at 10:56
  • in case of sqlite cli: sqlite> .header on .mode csv .output filename.csv SELECT * FROM table; .quit in case of java use: https://stackoverflow.com/questions/26563008/converting-sqlite-database-to-csv – XemyL Feb 14 '18 at 11:01