20

I'm new to Android programming and trying to wrap my head around this just to make myself clear about how things work.

When creating Sqlite databases in an Android app, where is the database stored? Does it get deleted when the app is removed? Any info about this would be much helpful in understanding Android programming for people coming from a web development background.

Zishan Neno
  • 2,647
  • 7
  • 34
  • 58

5 Answers5

26

SQlite databases are just files, and they're treated like any other file: they're stored (by default) in the application's private data area (/data/data/$PACKAGENAME/databases). They're deleted along with everything else in the application's private data area.

You can create a database on the SD card if you like. They, of course, won't be removed on uninstallation.

David Given
  • 13,277
  • 9
  • 76
  • 123
  • 5
    Actually, they too will be removed, if you used `getExternalFilesDir()` for the location. IOW, it depends where on external storage you put the database. – CommonsWare Mar 07 '12 at 13:59
  • Thanks for your reply, David. So just to get this right, they're stored as files rather than in a single file like in the case of MSSQL or MySql? – Zishan Neno Mar 07 '12 at 14:14
  • I don't know what MSSQL or MySQL does, but SQLite stores each database as a single file under the application's control. There is no central database storage. It's worth getting the sqlite3 command line tool and playing with it to get a feel for how it works (and it's useful for debugging, too). – David Given Mar 07 '12 at 14:24
1

Unless stated otherwise (by you), apps keep their datas under /data/data/<appname>, and SQLite-databases are in /data/data/<appname>/databases. When uninstalling an app, the whole directory tree of /data/data/<appname> will be deleted, including your databases.

bos
  • 6,437
  • 3
  • 30
  • 46
  • 1
    The directory is not `/data/data/appname/databases` - it's `/data/data/app.package.name/databases` – Aleks G Mar 07 '12 at 14:01
1

The DBs, Preferences, Cache Files, Temp Files all are stored in the location /data/data/app.package.name/ in Phone Memory.

When you uninstall your App, the whole Phone Memory folder for this app will be completely removed.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
0

The database is located in /data/data/app.package.name/databases . You can access this folder only on the emulator or on a rooted device (with a file explorer with a Super user right for example).

When you delete an app all the relative datas are deleted (databases included)

grunk
  • 14,718
  • 15
  • 67
  • 108
0

Normally, the database would be stored in

/data/data/package.name.of.your.app/databases

However it's not a good thing to rely on this. It's much better to find it using getDatabasePath on ContextWrapper, such as:

File dbFile = getDatabasePath(db_name);

And, yes, normally the whole directory /data/data/package.name.of.your.app would be deleted if you uninstall the app.

Aleks G
  • 56,435
  • 29
  • 168
  • 265