如何将现有数据库从一个应用程序复制到另一个应用程序 [英] How to copy existing database from one app to another

查看:180
本文介绍了如何将现有数据库从一个应用程序复制到另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的第一个应用程式建立了付费版本。



我想将现有的资料库复制到新的付费应用程式。



如何实现?



EDIT



在免费的应用程序我使用 SQLiteOpenHelper 。他们可以使用相同的数据库,但是想再次使用 SQLiteOpenHelper ,并且无法弄清如何使用其他数据库,因为应用程序具有不同的包名称如果他们使用相同的dosn't的免费数据库被删除,当应用程序被删除?)。



请注释,如果你想要更多的信息需要:))

解决方案

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




  • 因此,您可以将 内容提供商 转换为免费应用,然后允许付费版本从内容提供商收集数据,然后首先运行所有的数据。这在我看来有点麻烦 - 但是意味着用户不需要一个SD卡,你也可以控制数据,IE如果用户已经使用付费版本,你可以将数据添加到数据库,而不是替换


  • 您可以从免费版本的数据库保存到SD卡,然后收集它与付费版。根据您要编码的金额,您可以设置 广播接收器 ,然后 sendBroadcast() ,这样当免费应用程序接收到广播时,它将其数据库应用到SDcard,然后付费应用程序收集它。另一种方式是用户点击免费版本中的保存按钮,备份到SD卡,然后用户点击付费版本中的按钮,它会收到它 - 这可以像复制文件一样简单,替换应用程序的数据库,或者您可以将其作为不同的数据库导入付费应用程序,将其添加到主数据库中,然后丢弃它。




作为一个非常宽松和简单的指针,你可以将数据库复制到SD卡与这样的东西,它是从一个工作位的代码非常剥离,所以应该考虑未经测试到一定程度 - 您将需要在地方添加几个catch块以使其与清单中的SDcard的读写权限一起工作:

  //获取db:
InputStream myInput = new FileInputStream(/ data / data / com.package.name / databases / database-name);
//设置SD卡上的输出文件夹
文件目录= new File(/ sdcard / someFolderName);
//如果不存在,创建文件夹:
if(!directory.exists())
{
directory.mkdirs();
}
//设置输出文件流:
OutputStream myOutput = new FileOutputStream(directory.getPath()+/database-name.backup);
//将字节从输入文件传输到输出文件
byte [] buffer = new byte [1024];
int length;
while((length = myInput.read(buffer))> 0)
{
myOutput.write(buffer,0,length);
}
//关闭并清除流
myOutput.flush();
myOutput.close();
myInput.close();

Retreival非常相似:

  //设置db的位置:
OutputStream myOutput = new FileOutputStream(/ data / data / com.package.name / databases / database-name);
//设置SD卡上的文件夹
文件目录= new File(/ sdcard / someFolderName);
//设置输入文件流:
InputStream myInput = new FileInputStream(directory.getPath()+/database-name.backup);
//将字节从输入文件传输到输出文件
byte [] buffer = new byte [1024];
int length;
while((length = myInput.read(buffer))> 0)
{
myOutput.write(buffer,0,length);
}
//关闭并清除流
myOutput.flush();
myOutput.close();
myInput.close();然而,我建议先做一些检查,并质疑用户一点:

$ b


$ b

  • 检查SD卡上是否已存在文件,如果是,请覆盖它。

  • 检查他们是否要使用备份文件覆盖当前数据库

  • 您还需要执行一些检查,例如,是否安装了SD卡。



就像我说的上面的代码只是给你一个小提示,如何使用SD卡移动数据库。


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

解决方案

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.

这篇关于如何将现有数据库从一个应用程序复制到另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆