tags:

views:

603

answers:

5

I already have a sqlite database. And i put it in the "assets" folder of my project. Then what's the path of this databse. I read the document form adnroid. It said all the database in android,the path is data/data/pack_name/database_name. It make me confuse. I just place it in the "assets" folder. So the path is data/data/assets/database_name? Pls help .Thanks you very much

+3  A: 

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.

Anthony Forloney
"/data/data/YOUR_PACKAGE/databases/"But, what is the "YOUR_PACKAGE", It means :If my project name is HelloworldThen the path is "/data/data/Helloworld/databases/" ?Thanks
I believe that is correct, I am at work right now otherwise I would boot up my classes and give you the direct answer.
Anthony Forloney
Thanks very much
Not a problem, and see my edits for a revised example of the actual path.
Anthony Forloney
it would be this though with / instead of . - data/data/com/forloney/tracker/database
Donal Rafferty
er, good catch donal, will update. thanks
Anthony Forloney
I believe the previous version was right.
ccheneson
+2  A: 

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

ccheneson
A: 

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.

ScCrow
A: 

@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();
Dan
A: 

This is a good question, since an existing database and a newly created one are handled differently.

I've faced the same problem, and after much reading, I found a great explanation from downloadandroid.info

In part it says:

If you want to include a database with initial data in your apk, you have to insert the database file into the projects assets folder, then programmatically check the database, and if it does not exist copy the one from the assets.

The full details are here http://downloadandroid.info/2010/05/default-database/

Noah