11

I need to make an offline backup of the SQLite database in the mobile's internal storage or the SD Card. But I am clueless, how's this possible.

I followed many threads here on SO, but they suggest copying the DB one location to other, which is not suitable for me, neither to the Google Drive.

Any guidance would be precious.

8
  • So what would be suitable for you then? Commented Nov 12, 2018 at 16:39
  • 1. copy your db to sdcard 2. if reinstall, copy db back to the app. Commented Nov 12, 2018 at 16:47
  • stackoverflow.com/questions/1995320/… Commented Nov 12, 2018 at 16:50
  • @earthw0rmjim Any approach in which I can get the original state retained without the network. Commented Nov 12, 2018 at 17:15
  • @RakeshKumar I already checked that thread, but I am not able to copy anything in my emulator. (Not tried in any physical device). Commented Nov 12, 2018 at 17:17

2 Answers 2

4

I used this approach.

public string DATABASE_NAME = "YOUR DATABASE NAME HERE";

   public void exportDB(){
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                Log.d("TAG", "DatabaseHandler: can write in sd");
                //Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME
                String currentDBPath = "filepath here"+DATABASE_NAME;
                //Replace with YOUR_FOLDER_PATH and TARGET_DB_NAME in the SD card
                String copieDBPath = DATABASE_NAME;
                File currentDB = new File(data, currentDBPath);
                File copieDB = new File(sd, copieDBPath);
                if (currentDB.exists()) {
                    Log.d("TAG", "DatabaseHandler: DB exist");
                    @SuppressWarnings("resource")
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    @SuppressWarnings("resource")
                    FileChannel dst = new FileOutputStream(copieDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch  (Exception e) {
            e.printStackTrace();
        }

    } 
Sign up to request clarification or add additional context in comments.

2 Comments

//Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME, no need to hard code package name if you you use String currentDBPath = context.getDatabasepath(database).getPath(); where context is a Context and database is the database name.
@MohammedsalimShivani restoring from such a file (basically reversing the backup) does work (although you may find that you need to, or that it's simpler to then restart the App). However I'd suggest, when restoring, to initially make a copy of the original DB (so that should the restore fail you can revert to the original DB).
2

Finally, with this GitHub sample I successfully implemented whatever customization I needed. And it's live now.

6 Comments

I couldn't run that sample. Can you share how you did it? A kind of sqlite file google drive could not backup. Thank you in advance.
What trouble are you facing? I haven't tried the Google Drive backup, as I didn't need that.
I tried to back up the sqlite database in the assets folder to google drive, but I was not successful. GDA Api has been used by google last month.
Is your sqlite DB residing in the assets folder?
Yeah. Inside the Assets folder.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.