Skip to content Skip to sidebar Skip to footer

How To Copy Existing Database From One App To Another

I have been creating a pay version of my first app. I would like to copy the existing database to the new payed app. How is this possible? EDIT In the free app I am using a SQLiteO

Solution 1:

You have a couple of options, you won't get a package with a different name to directly interact with another packages database.

  • So you could code a Content Provider into the free app, then allow the paid version to collect data from the content provider, then on first run pass all the data across. This is somewhat cumbersome in my opinion - but would mean the user doesn't need an SDcard, you also stay in control of the data, IE if the user has already used the paid version you can ADD the data onto the database rather than replacing the new one with the old free one...

  • You could save the database to the SDcard from within the free version, and then collect it with the paid version. Depending how much you want to code, you could set up a Broadcast Receiver in the free app, and then sendBroadcast() from the paid version, that way when the free app receives a broadcast it copes its database to the SDcard, then the paid app collects it. The other way would be for the user to click a save button in the free version, backup to the SDcard, then the user clicks a button in the paid version and it receives it - this can be as simple as copying the file and replacing the app's database, or you could import it to the paid app as a different database, process it adding what you need to the main database then discard it.

As a very loose and simple pointer, you can copy the database to the SDcard with something like this, it is very stripped down from a working bit of code, so should be considered untested to a degree - you will need to add a few catch blocks in places to get it working along with the read write permissions for the SDcard in the manifest:

// Get hold of the db:InputStreammyInput=newFileInputStream("/data/data/com.package.name/databases/database-name");
    // Set the output folder on the SDcardFiledirectory=newFile("/sdcard/someFolderName");
    // Create the folder if it doesn't exist:if (!directory.exists()) 
    {
        directory.mkdirs();
    }     
    // Set the output file stream up:OutputStreammyOutput=newFileOutputStream(directory.getPath()+ "/database-name.backup");
    // Transfer bytes from the input file to the output filebyte[] buffer = newbyte[1024];
    int length;
    while ((length = myInput.read(buffer))>0)
    {
        myOutput.write(buffer, 0, length);
    }
    // Close and clear the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

Retreival is very similar:

// Set location for the db:OutputStreammyOutput=newFileOutputStream("/data/data/com.package.name/databases/database-name");
    // Set the folder on the SDcardFiledirectory=newFile("/sdcard/someFolderName");
    // Set the input file stream up:InputStreammyInput=newFileInputStream(directory.getPath()+ "/database-name.backup");
    // Transfer bytes from the input file to the output filebyte[] buffer = newbyte[1024];
    int length;
    while ((length = myInput.read(buffer))>0)
    {
        myOutput.write(buffer, 0, length);
    }
    // Close and clear the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

However I would suggest doing some checks first and questioning the user a little:

  • Check if a file already exists on the SDcard, if so do they want to overwrite it.
  • Check they want to overwrite the current database with the backup one
  • You will also need to do some checks like, is the SDcard mounted ect.

Like I say the code above is really just to give you a little hint as to how you could possibly use the SDcard to move the database.

Post a Comment for "How To Copy Existing Database From One App To Another"