1. Are you ready for the Galaxy S20? Here is everything we know so far!

Importing SQLite Database

Discussion in 'Android Development' started by AndyCr15UK, Jul 2, 2017.

  1. AndyCr15UK

    AndyCr15UK Lurker
    Thread Starter

    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 (Text):
    1.  
    2. private void exportDB(){
    3.        File sd = Environment.getExternalStorageDirectory();
    4.        File data = Environment.getDataDirectory();
    5.        FileChannel source=null;
    6.        FileChannel destination=null;
    7.        String currentDBPath = "/data/"+ "com.androidandyuk.regularreminders" +"/databases/Reminders";
    8.        String backupDBPath = "Reminders.db";
    9.        File currentDB = new File(data, currentDBPath);
    10.        File backupDB = new File(sd, backupDBPath);
    11.        try {
    12.            source = new FileInputStream(currentDB).getChannel();
    13.            destination = new FileOutputStream(backupDB).getChannel();
    14.            destination.transferFrom(source, 0, source.size());
    15.            source.close();
    16.            destination.close();
    17.            Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
    18.        } catch(IOException e) {
    19.            e.printStackTrace();
    20.            Toast.makeText(this, "Exported Failed!", Toast.LENGTH_LONG).show();
    21.        }
    22.    }
    23.  
    Can anyone help with how I would import it again?

    Thanks!
     


    #1 AndyCr15UK, Jul 2, 2017
    Last edited by a moderator: Jul 2, 2017
  2. Best Answer:
    Post #2 by Deleted User, Jul 2, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Importing SQLite Database
  1. Oldschool297
    Replies:
    2
    Views:
    422
  2. ghostanime2001
    Replies:
    0
    Views:
    617
  3. hussanhijazi
    Replies:
    0
    Views:
    1,229
  4. tiho_bg
    Replies:
    0
    Views:
    1,830
  5. unet
    Replies:
    0
    Views:
    2,500
  6. DonaldDuck313
    Replies:
    0
    Views:
    2,243
  7. Yaron21
    Replies:
    8
    Views:
    825
  8. Motorhomer14
    Replies:
    0
    Views:
    1,681
  9. mawallace
    Replies:
    1
    Views:
    4,060
  10. raywood
    Replies:
    1
    Views:
    914

Share This Page

Loading...