我可以指定sqlite数据库的路径

时间:2016-02-04 12:58:38

标签: android sqlite

我在android中创建了sqlite数据库,如下面的代码所示,我添加了一些记录。然后我想复制该数据库以在其他地方使用它 应用程序,我的数据库被称为“GEOLOC.db”,我搜索它,但它没有找到,尽管它包含数据。

请让我知道

1 - 如何知道sqlite数据库的保存位置

2 - 我可以指定数据库保存的路径吗?

public class SQLiteHelper extends SQLiteOpenHelper {

private final String TAG = this.getClass().getSimpleName();

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "GEOLOC.db";//can i specify a pth here??
private static final String DATABASE_TABLE_NAME = "NODE_00";

private Context mCtx = null;

public SQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mCtx = context;
}

1 个答案:

答案 0 :(得分:0)

通常,特定的sqlite数据库特定于单个应用程序。但是,除非手机已植根,否则您无法查看它。实际上,它通常位于以下路径中:

//data/data/<Your-Application-Package-Name>/databases/<your-database-name>

可以使用以下代码访问应用程序包名称:

PACKAGE_NAME = getApplicationContext().getPackageName();

数据库名称存储在通过以下声明扩展SQLiteOpenHelper的类中:

private static final String DATABASE_NAME = "buspass";

为了在应用程序之间共享数据(证明它们都是由您开发的),您需要在两个应用程序的清单文件中指定共享用户ID。

在两个应用中使用相同的DBAdapter。在托管数据库的应用程序中,使用本机上下文调用DBAdapter

DBadapter hostDBAdapter = new DbAdapter(getApplicationContext());
performerDBadapter.open();
In the second app, access the database with the context of the database hosting app.
First, define the shared context:

Context sharedContext = null;
    try {
        sharedContext = this.createPackageContext("replace.with.host.package.name", Context.CONTEXT_INCLUDE_CODE);
        if (sharedContext == null) {
            return;
        }
    } catch (Exception e) {
        String error = e.getMessage(); 
        return;
        }   

然后使用共享上下文打开DBAdapter

DbAdapter sharedDBadapter = new PerformerDbAdapter(sharedContext);
sharedDBadapter.open();

清单文件应包含以下代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:sharedUserId="my.app" ... >

希望这会有所帮助:)