Skip to content Skip to sidebar Skip to footer

How Does Android Access A Sqlite Database Included In The Assets Folder

I already have a SQLite database. I put it in the assets folder of my project. I read the Android documentation. It said that for all the databases in Android, the path is data/dat

Solution 1:

The package name is not the project name, the package name is the namespace. From Anthony's link.

Remember to change the "YOUR_PACKAGE" to your application package namespace (i.e: com.examplename.myapp) in the DB_PATH string.

For example, from the Hello World tutorial, the project name is HelloAndroid but the package name is com.example.helloandroid

If this application had a database, it would be stored at data/data/com.example.helloandroid/database

To see how it is for the other applications you can start your emulator. On the menu bar you have your avd's name (I think it stands for Android Virtual Device). On mine it s "avdessay:5554"

(On Linux) From command line, type:

adb -s emulator-5554 shell

You have to replace 5554 by whatever port you are using.

if you have the command prompt '#' you can type:

cd data/data

There, you will see that eveything is in a form of a package name.

More info here


Solution 2:

When you create a database by utilizing the SQLiteDatabase or SQLiteOpenHelper classes, it creates the database in your data/data/package_name/database.

You can access that resource by using

InputStream myInput = myContext.getAssets().open(your_database_here);

Any other information, look at Using your own SQLite database in Android Applications

The package_name portion of the path, would be the name of your package. You can find the name of the package at the first line in your .java files.

As an example, my class starts with this at the top

package com.forloney.tracker;

So my database is in data/data/com.forloney.tracker/database folder.

Hope this makes sense.


Solution 3:

@ScCrow I too followed this example and had the same problems you did until I realized I was not using the DataBaseHelper correctly (or rather it had a quirk I overlooked).

When you use your DatabaseHelper class in your activity, you have to make sure you call createDatabase first! If you look at the code for openDatabase it does NOT check to see if the database exists, so you either have to (attempt to) create the database in each activity you use it in, or modify the openDatabase method to check to make sure the db exists. The link posted does actually instruct you to use it this way but you (like me) may have glossed over that.

Bad:

DBAdapter db = new DBAdapter(this);
db.openDataBase(); //Bad! db not created yet!

Good:

DBAdapter db = new DBAdapter(this);
db.createDataBase(); //needs exception handling
db.openDataBase();

Solution 4:

When I try to open my DB, I get "unable to open database file". I assume its not finding the DB and not some other programmer error. In the log I see the following which looks good to me.

   sqllite3_open_v2("/data/data/com.isildo.HelloListView/databases/ListsDB" ...

This is the setup

private static String DB_PATH = "/data/data/com.isildo.HelloListView/databases/";    
private static String DB_NAME = "ListsDB";

In my projects assets in the Package Explorer, I see the ListsDb database.

So I at least think I have it all correct. I am using the example at [http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/]

In one of the posts here, someone offers a suggestion about setting some Assets parameters. {no response to post there}

**To get an ASSETS folder into your APK:
  In /nbproject/project.properties, 
     change assets.dir=
to
     assets.dir=assets 
     assets.available=true
In /nbproject/build-impl.xml, there is line in the “if=assets.available” target that reads
that needs to be changed to**

Is this something we need to do, and if so, can we get a little better direction on the changes required. I could not find the places to make the suggested changes I looked at the project settings, and other things.

Yep, Im new to the environment, so I may just be not finding them. Im using Eclipse on windows.


Solution 5:

In your DBAdapter.java, change the return type of openDatabase method to SQLiteDatabase.

When you access the database simply use SQLiteDatabase data = db.openDatabase(), where db is DBAdapter db = new DBAdapter(this).


Post a Comment for "How Does Android Access A Sqlite Database Included In The Assets Folder"