-3

I created sqlite database in android as shown in the code below and i added some records to it. then i wanted to copy that database to use it in anothe application, my database is called "GEOLOC.db" and i searched for it but it was not found despite it contains data.

please let me know

1-how to know where the sqlite database is saved

2-can i specify a path to which the databse will be saved?

code:

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;
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 3
    Possible duplicate of [How to use an existing database with an Android application](http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application) – 323go Feb 04 '16 at 12:59
  • 1
    "how to know where the sqlite database is saved" -- [call `getDatabasePath("GEOLOC.db")`](http://developer.android.com/reference/android/content/Context.html#getDatabasePath%28java.lang.String%29) on some handy `Context`. "can i specify a path to which the databse will be saved?" -- I think that works as of API Level 8 or 9, though usually there is no need to change the path. – CommonsWare Feb 04 '16 at 13:00

1 Answers1

0

Usually, a particular sqlite database is specific to one single application. However you won't be able to view it unless the phone is rooted. In fact it is normally found in the following path:

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

The application package name can be accessed using the following code:

PACKAGE_NAME = getApplicationContext().getPackageName();

The database name is stored in the class which extends the SQLiteOpenHelperthrough the following declaration:

private static final String DATABASE_NAME = "buspass";

In order to share data between apps (provied that they have both been developed by you), you will need to specify a shared user id in the Manifest file of both apps.

Use the same DBAdapter in both apps. In the app that hosts the database, call the DBAdapter with the native context.

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;
        }   

Then open the DBAdapter with the shared context:

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

The manifest file should have the following code:

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

Hope this helps :)

Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79