1

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 SQLiteOpenHelper. They can use the same database but would like to use the SQLiteOpenHelper again and can't figure out how to get this to use the other database as the apps have different package names (If they use the same dosn't the free database get deleted when the app gets deleted?).

Please comment if you would like additional info (and perhaps what info you need:))

Bastaix
  • 835
  • 3
  • 12
  • 23
  • Without some more detail, it's impossible to say. Is there a reason they can't connect to the same database? – razlebe Apr 14 '11 at 10:59

1 Answers1

6

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:
    InputStream myInput = new FileInputStream("/data/data/com.package.name/databases/database-name");
    // Set the output folder on the SDcard
    File directory = new File("/sdcard/someFolderName");
    // Create the folder if it doesn't exist:
    if (!directory.exists()) 
    {
        directory.mkdirs();
    }     
    // Set the output file stream up:
    OutputStream myOutput = new FileOutputStream(directory.getPath()+ "/database-name.backup");
    // Transfer bytes from the input file to the output file
    byte[] buffer = new byte[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:
    OutputStream myOutput = new FileOutputStream("/data/data/com.package.name/databases/database-name");
    // Set the folder on the SDcard
    File directory = new File("/sdcard/someFolderName");
    // Set the input file stream up:
    InputStream myInput = new FileInputStream(directory.getPath()+ "/database-name.backup");
    // Transfer bytes from the input file to the output file
    byte[] buffer = new byte[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.

Scoobler
  • 9,696
  • 4
  • 36
  • 51
  • Thank you for the very detailed reply. Think I will try the solution with the Content Provider, as I know people using the app without a SD card. Maybe I can learn something on the way as well:) – Bastaix Apr 15 '11 at 08:09
  • @Bastaix, Hi, could you share your solution please! I'm trying to implement exactly the same thing through `FileProvider` without luck. Got `SecurityException`s wherever I go. – WindRider Feb 18 '16 at 14:51