将现有的SQLite数据库导入Android应用程序 [英] Import Existing SQLite Database into Android Application

查看:238
本文介绍了将现有的SQLite数据库导入Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道有人问过类似的问题,但我找不到适合我具体情况的东西.

我有一个创建数据库并填充数据库等的应用程序,但是我在设备上没有超级用户访问权限,所以这是我的问题.我需要使用一些值填充此数据库,然后将其导入到我的应用程序/设备中.

如果我具有root用户访问权限,则可以直接替换/data/data/com.my.application/databases文件夹中的数据库,但是由于我没有root用户访问权限,因此我需要能够导入该数据库我转到了这个位置,并替换了标准位置.

有人可以帮助我提供一些代码或如何将数据库导入此文件夹的示例吗?我只需要能够导入和替换现有的.

谢谢, 威汉(Wihan)

解决方案

您可以使用此库在设备上使用导入的数据库:

https://github.com/jgilfelt/android-sqlite-asset-helper

如文档中所述,您只需要将数据库文件放在资产文件夹下的名为数据库/assets/databases/database.db

的目录下

然后创建一个从SQLiteAssetHelper扩展的DBHelper类,如下所示:

public class DBHelper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "example.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

}

您现在可以通过内容提供商和所有内容访问数据.

您还可以使用 http://sqlitebrowser.org/在计算机上创建预加载的数据库./p>

希望有帮助.

So I know similar questions have been asked but I cannot find something that is for my exact case.

I have an application that creates a database and populates it etc but I have no root access on my device, so here is my problem. I need to populate this database with some values and then import this into my application/device.

If I had root access I could simply go and replace the database in the /data/data/com.my.application/databases folder, but since I do not have root access I need to be able to import this database that I changed into this location and replace the standard one.

Can anyone help me along with some code or an example of how to import a database into this folder? I just need to be able to import and replace the existing one.

Thanks, Wihan

解决方案

You can use this library to use an imported database on your device:

https://github.com/jgilfelt/android-sqlite-asset-helper

As stated on the documentation you just need to put your database file on the assets folder under a directory called databases /assets/databases/database.db

Then make a DBHelper class that extends from SQLiteAssetHelper like this:

public class DBHelper extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "example.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

}

And you can access now to your data with content providers and all the stuff.

Also you can use http://sqlitebrowser.org/ for creating your pre loaded database on your computer.

Hope it helps.

这篇关于将现有的SQLite数据库导入Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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