如何从一个应用程序复制现有的数据库到另一应用程序、数据库

2023-09-07 00:36:10 作者:用力爱过的人不该计较

我一直在创造我的第一个应用程序付费版本。

I have been creating a pay version of my first app.

我想现有的数据库复制到新纳了应用程序。

I would like to copy the existing database to the new payed app.

这怎么可能?

修改

在免费的应用程序,我使用的是 SQLiteOpenHelper 。他们可以使用相同的数据库,但想了 SQLiteOpenHelper 再次使用,不能找出如何得到这个使用其他数据库的应用程序有不同的包名(如果他们使用相同的不到风度,当应用程序被删除的免费数据库被删除?)。

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:))

推荐答案

您有几个选项,你不会得到一包用不同的名称直接与另一包数据库进行交互。

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

所以,你可以codeA 内容提供商 到免费的应用程序,然后让付费版本,从内容提供商收集数据,然后在第一次运行整个传递的所有数据。这在我看来是有点麻烦 - 但将意味着用户不需要的SD卡,你还停留在对数据的控制,即如果用户已经使用了付费版本,你可以添加数据到数据库,而不是取代在新与旧的免费的......

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...

您可以将数据库保存到SD卡中,从免费版本,然后再与付费版本收集。根据你要多少code,你可以建立一个 广播接收器 在免费的应用程序,然后sendBroadcast()从付费版本,在免费应用程序接收它对付它的数据库到SD卡广播的方式,那么付费应用程序收集它。另一种方法将是为用户点击保存按钮在免费版,备份到SD卡,则用户点击付费版本一个按钮,它接收它 - 这可以是简单的复制文件和更换应用程序的数据库,或者你可以将其导入到付费应用程序为不同的数据库,处理它加入你需要到主数据库是什么,然后将其丢弃。

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.

作为一个非常松散,简单的指针,你可以将数据库复制到SD卡与这样的事情,这是非常剥离下来从$​​ C $ C一个工作位,因此应被视为未经检验的程度 - 你会需要的地方增加一些catch块来得到它的工作以及在清单中的SD卡的读写权限:

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是非常相似的:

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:

检查文件的SD卡已经存在,如果这样做,他们要覆盖它。 检查他们是否要覆盖当前数据库的备份一个 您也需要做一些检查一样,是SD卡安装等。

就像我说的code以上实际上只是给你一个小提示,如何你可能使用SD卡移动数据库。

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.