سؤال

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?

هل كانت مفيدة؟

المحلول

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");

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top