Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does android access a sqlite database included in the assets folder

Tags:

android

sqlite

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/data/pack_name/database_name.

This confused me. I just placed it in the assets folder, so the path is data/data/assets/database_name?

like image 775
user275788 Avatar asked Feb 23 '10 14:02

user275788


2 Answers

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

like image 82
ccheneson Avatar answered Sep 25 '22 14:09

ccheneson


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.

like image 29
Anthony Forloney Avatar answered Sep 26 '22 14:09

Anthony Forloney