1

I have an sqlite database (.db), copied it in assets folder and have to copy it to /data/data/package_name/databases. The problem is I don't have databases folder and did not succeed to create it.

String dirPath = "/data/data/com.gatec.douaa/databases/";
Log.d("Directory",dirPath);
File projDir = new File(dirPath);
 if (!projDir.exists())
        projDir.mkdirs();

Can you please help me to create that folder to store on it the database? Thank you very much.

androniennn
  • 3,117
  • 11
  • 50
  • 107
  • 3
    Please consider using `SQLiteAssetHelper`, which handles all of this for you: https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare Sep 27 '12 at 17:25
  • @CommonsWare: so no need to copy the database to /databases folder? Excellent will try that :). – androniennn Sep 27 '12 at 17:34
  • The db still needs to be copied there, just the heavy lifting of doing that is done for you. – Barak Sep 27 '12 at 17:56
  • @CommonsWare: please see the actual problem:`http://stackoverflow.com/questions/12628014/cant-open-database-with-sqliteassethelper` – androniennn Sep 27 '12 at 18:40
  • http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 – Yaqub Ahmad Sep 28 '12 at 04:50

1 Answers1

0

In my experieces, the "databases" directory is created the first time you use the database. So, if you want copy the database from "asset" directory, you have to force the database creation in the path "data/data/package_name/databases". This happens only in the lollipop (android 5), in my experiences. Then you can use the code below:

File file = new File(context.getDatabasePath("database_name.db").getPath());
    if (!file.exists())
    {
        try
        {
            file.createNewFile();
        }
        catch (IOException e)
        {

        }
    }
Dante
  • 99
  • 1
  • 2