#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2017
    Posts
    2
    Rep Power
    0

    Question Importing SQLite Database


    Hi all, I have an app which I would like to export the SQLite database and import it again, like a backup and restore feature. I have code that exports the database to the SD Card like this -

    Code:
    private void exportDB(){
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            FileChannel source=null;
            FileChannel destination=null;
            String currentDBPath = "/data/"+ "com.androidandyuk.regularreminders" +"/databases/Reminders";
            String backupDBPath = "Reminders.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);
            try {
                source = new FileInputStream(currentDB).getChannel();
                destination = new FileOutputStream(backupDB).getChannel();
                destination.transferFrom(source, 0, source.size());
                source.close();
                destination.close();
                Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
            } catch(IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "Exported Failed!", Toast.LENGTH_LONG).show();
            }
        }
    Can anyone help with how I would import it again?

    Thanks!
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,123
    Rep Power
    2010
    Have you done any search for an answer?
    Have you tried with a "reverse" copy?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2017
    Posts
    2
    Rep Power
    0
    Lots of searching, which mostly told me about placing a DB into an assets folder.

    When I have time later I will search about reverse copy. Thanks.

    Someone else is telling me just save it direct to a server instead of the database, which might make sense.

IMN logo majestic logo threadwatch logo seochat tools logo