Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backup and restore SQLite database to sdcard

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?

like image 231
chrisonline Avatar asked Jan 31 '10 00:01

chrisonline


People also ask

How do I backup SQLite database on Android?

1. copy your db to sdcard 2. if reinstall, copy db back to the app. @earthw0rmjim Any approach in which I can get the original state retained without the network.

Can I store SQLite database on Google Drive?

There is also a button which will show the data from google drive and user can edit and update the data so the updated data is then store in the google drive. I'm new in Android development please help me. you can compress your database file in ZIP and send that zip to server. then send that .


1 Answers

Here is my code:

    // Local database
    InputStream input = new FileInputStream(from);

    // create directory for backup
    File dir = new File(DB_BACKUP_PATH);
    dir.mkdir();

    // Path to the external backup
    OutputStream output = new FileOutputStream(to);

    // transfer bytes from the Input File to the Output File
    byte[] buffer = new byte[1024];
    int length;
    while ((length = input.read(buffer))>0) {
        output.write(buffer, 0, length);
    }

    output.flush();
    output.close();
    input.close();
like image 116
chrisonline Avatar answered Oct 25 '22 14:10

chrisonline