2

I am using SQLiteOpenHelper to create my database. I changed the constructor like this:

public SQLite(Context context){
    super(context, "/mnt/sdcard"+DATABASE_NAME+".db", null, DATABASE_VERSION);
}

The database is created in the public directory just fine. The problem is that when I try to execute functions, I cant change the database to the one I created in the public directory. How can I change this? eg:

@Override
public void onCreate(SQLiteDatabase db){
    Log.i("DB PATH", db.getPath());
}

This prints out:

DB PATH - /data/data/package_name/databases/database_name

I need this to print out the path to the public database.

Thank you in advance.

EDIT

Copy private DB to public directory instead

Changed constructor to this:

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

Used the code from the link that Geralt suggested:

private void copyDataBase(String dbname) throws IOException {
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(dbname);
    // Path to the just created empty db
    String outFileName = "/data/data/com.sample.view/databases/" + dbname;
    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

Here is the stack trace of what happens:

04-15 09:57:55.275: W/ResourceType(11994): Invalid package identifier when getting bag for resource number 0xffffffff
04-15 09:57:55.275: W/ResourceType(11994): Invalid package identifier when getting bag for resource number 0xffffffff
04-15 09:57:55.275: W/ResourceType(11994): Invalid package identifier when getting bag for resource number 0xffffffff
04-15 09:57:55.320: W/System.err(11994): java.io.FileNotFoundException: aCollectDatabase
04-15 09:57:55.320: W/System.err(11994): at android.content.res.AssetManager.openAsset(Native Method)
04-15 09:57:55.320: W/System.err(11994): at android.content.res.AssetManager.open(AssetManager.java:315)
04-15 09:57:55.320: W/System.err(11994): at android.content.res.AssetManager.open(AssetManager.java:289)
04-15 09:57:55.320: W/System.err(11994): at co.za.datasolve.acollect.ACollectActivity.copyDataBase(ACollectActivity.java:184)
04-15 09:57:55.320: W/System.err(11994): at co.za.datasolve.acollect.ACollectActivity.onCreate(ACollectActivity.java:153)

And this is the problem line:

InputStream myInput = getApplicationContext().getAssets().open(dbname);

Does anyone have suggestions of how I can fix this?

Lunchbox
  • 1,538
  • 2
  • 16
  • 42

2 Answers2

2

I changed the constructor like this:

Problem is that in constructor you're specifing only database name and not path. Database created by SQLiteOpenHelper will be placed always in internal storage for security reasons - you can't change this logic. It's build-in logic by OS.

All what you can do is explicitly copy your database into another directory.

In this thread you'll find how to copy database.

Update:

Here is how to get database path:

String path = getApplicationContext().getDatabasePath("your_database_name");
Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • I have edited my answer. I tried using your suggestion, but I keep getting an error. See my edit please – Lunchbox Apr 15 '14 at 08:11
  • @Lunchbox com.sample.view this only example package you need to use your package. [here](http://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere) is how you can obtain your package name. – Simon Dorociak Apr 15 '14 at 08:17
  • Sorry, I copied that from the link you sent me. I am entering my correct path and package name – Lunchbox Apr 15 '14 at 08:22
  • The problem is this:InputStream myInput = getApplicationContext().getAssets().open(dbname); – Lunchbox Apr 15 '14 at 08:23
  • @Lunchbox FileNotFoundException means that file is not in provided path. Check your db path if it's correct. – Simon Dorociak Apr 15 '14 at 09:11
  • In my SQLite class I checked it like this: Log.d("DB Path", db.getPath());, I try to check it in DDMS, but I cant open the path, probably because I am not a root user. I need this to work on non rooted devices – Lunchbox Apr 15 '14 at 09:36
  • @Lunchbox Problem is with incorrect db path. Did you specify db path like this: `String outFileName = "/data/data/" + getApplicationContext().getPackageName() + "/databases/" + dbname;` ? – Simon Dorociak Apr 15 '14 at 11:47
  • I solved the issue, will mark your answer as correct. Only with one slight change. I just got the path like this instead: getApplicationContext().getDatabasePath("my_database_name"); Can you add it as add edit to your answer? – Lunchbox Apr 15 '14 at 12:35
  • @Lunchbox also this is solution ;) nice touch and workaround. – Simon Dorociak Apr 15 '14 at 12:42
  • Thanks, just changed the title as well. Realised that the question changed somewhat during the discussion – Lunchbox Apr 15 '14 at 12:50
0

You should use Environment.getExternalStorageDirectory() to get the path to external storage.

Use this:

    public SQLite(Context context){
        super(context, Environment.getExternalStorageDirectory() + File.Separator +  
              DATABASE_NAME+".db", null, DATABASE_VERSION);

    }

Don't hard code the path.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34