Backup SQLite Database in Android












0














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.










share|improve this question






















  • So what would be suitable for you then?
    – earthw0rmjim
    Nov 12 at 16:39










  • 1. copy your db to sdcard 2. if reinstall, copy db back to the app.
    – Kingfisher Phuoc
    Nov 12 at 16:47










  • stackoverflow.com/questions/1995320/…
    – Rakesh Kumar
    Nov 12 at 16:50










  • @earthw0rmjim Any approach in which I can get the original state retained without the network.
    – Mohammedsalim Shivani
    Nov 12 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).
    – Mohammedsalim Shivani
    Nov 12 at 17:17
















0














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.










share|improve this question






















  • So what would be suitable for you then?
    – earthw0rmjim
    Nov 12 at 16:39










  • 1. copy your db to sdcard 2. if reinstall, copy db back to the app.
    – Kingfisher Phuoc
    Nov 12 at 16:47










  • stackoverflow.com/questions/1995320/…
    – Rakesh Kumar
    Nov 12 at 16:50










  • @earthw0rmjim Any approach in which I can get the original state retained without the network.
    – Mohammedsalim Shivani
    Nov 12 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).
    – Mohammedsalim Shivani
    Nov 12 at 17:17














0












0








0







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.










share|improve this question













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.







android sqlite android-sqlite






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 16:35









Mohammedsalim Shivani

74221123




74221123












  • So what would be suitable for you then?
    – earthw0rmjim
    Nov 12 at 16:39










  • 1. copy your db to sdcard 2. if reinstall, copy db back to the app.
    – Kingfisher Phuoc
    Nov 12 at 16:47










  • stackoverflow.com/questions/1995320/…
    – Rakesh Kumar
    Nov 12 at 16:50










  • @earthw0rmjim Any approach in which I can get the original state retained without the network.
    – Mohammedsalim Shivani
    Nov 12 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).
    – Mohammedsalim Shivani
    Nov 12 at 17:17


















  • So what would be suitable for you then?
    – earthw0rmjim
    Nov 12 at 16:39










  • 1. copy your db to sdcard 2. if reinstall, copy db back to the app.
    – Kingfisher Phuoc
    Nov 12 at 16:47










  • stackoverflow.com/questions/1995320/…
    – Rakesh Kumar
    Nov 12 at 16:50










  • @earthw0rmjim Any approach in which I can get the original state retained without the network.
    – Mohammedsalim Shivani
    Nov 12 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).
    – Mohammedsalim Shivani
    Nov 12 at 17:17
















So what would be suitable for you then?
– earthw0rmjim
Nov 12 at 16:39




So what would be suitable for you then?
– earthw0rmjim
Nov 12 at 16:39












1. copy your db to sdcard 2. if reinstall, copy db back to the app.
– Kingfisher Phuoc
Nov 12 at 16:47




1. copy your db to sdcard 2. if reinstall, copy db back to the app.
– Kingfisher Phuoc
Nov 12 at 16:47












stackoverflow.com/questions/1995320/…
– Rakesh Kumar
Nov 12 at 16:50




stackoverflow.com/questions/1995320/…
– Rakesh Kumar
Nov 12 at 16:50












@earthw0rmjim Any approach in which I can get the original state retained without the network.
– Mohammedsalim Shivani
Nov 12 at 17:15




@earthw0rmjim Any approach in which I can get the original state retained without the network.
– Mohammedsalim Shivani
Nov 12 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).
– Mohammedsalim Shivani
Nov 12 at 17:17




@RakeshKumar I already checked that thread, but I am not able to copy anything in my emulator. (Not tried in any physical device).
– Mohammedsalim Shivani
Nov 12 at 17:17












2 Answers
2






active

oldest

votes


















1














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();
}

}





share|improve this answer





















  • And restoring the backup with this file works??
    – Mohammedsalim Shivani
    Nov 12 at 17:14










  • //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.
    – MikeT
    Nov 12 at 18:52












  • @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).
    – MikeT
    Nov 12 at 19:16



















0














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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266434%2fbackup-sqlite-database-in-android%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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();
    }

    }





    share|improve this answer





















    • And restoring the backup with this file works??
      – Mohammedsalim Shivani
      Nov 12 at 17:14










    • //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.
      – MikeT
      Nov 12 at 18:52












    • @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).
      – MikeT
      Nov 12 at 19:16
















    1














    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();
    }

    }





    share|improve this answer





















    • And restoring the backup with this file works??
      – Mohammedsalim Shivani
      Nov 12 at 17:14










    • //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.
      – MikeT
      Nov 12 at 18:52












    • @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).
      – MikeT
      Nov 12 at 19:16














    1












    1








    1






    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();
    }

    }





    share|improve this answer












    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();
    }

    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 16:46









    astric mobiles

    14312




    14312












    • And restoring the backup with this file works??
      – Mohammedsalim Shivani
      Nov 12 at 17:14










    • //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.
      – MikeT
      Nov 12 at 18:52












    • @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).
      – MikeT
      Nov 12 at 19:16


















    • And restoring the backup with this file works??
      – Mohammedsalim Shivani
      Nov 12 at 17:14










    • //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.
      – MikeT
      Nov 12 at 18:52












    • @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).
      – MikeT
      Nov 12 at 19:16
















    And restoring the backup with this file works??
    – Mohammedsalim Shivani
    Nov 12 at 17:14




    And restoring the backup with this file works??
    – Mohammedsalim Shivani
    Nov 12 at 17:14












    //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.
    – MikeT
    Nov 12 at 18:52






    //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.
    – MikeT
    Nov 12 at 18:52














    @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).
    – MikeT
    Nov 12 at 19:16




    @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).
    – MikeT
    Nov 12 at 19:16













    0














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






    share|improve this answer


























      0














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






      share|improve this answer
























        0












        0








        0






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






        share|improve this answer












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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 16:03









        Mohammedsalim Shivani

        74221123




        74221123






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266434%2fbackup-sqlite-database-in-android%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly