Location of sqlite database on the device
Asked Answered
W

20

107

I've created a sqlite database programmatically with the default way of extending SQLiteOpenHelper and overriding onCreate(). This way the db gets created on the fly when needed.

I'd like to check the contents of the db file on my OS X machine with a sqlite browser. I know the name of the db file, but I can't find it on the device. I've connected to the device via USB and looked with finder and terminal, but I just can't find the db file.

What is the default location for a sqlite databases on an android device?

Whitehead answered 15/12, 2010 at 16:47 Comment(1)
See here:programatically get the database pathMystic
A
102

You can find your created database, named <your-database-name>

in

//data/data/<Your-Application-Package-Name>/databases/<your-database-name>

Pull it out using File explorer and rename it to have .db3 extension to use it in SQLiteExplorer

Use File explorer of DDMS to navigate to emulator directory.

Antoninus answered 15/12, 2010 at 16:58 Comment(5)
When I try that in an emulator, I can cd to /data. But when I try that with my connected Galaxy Vibrant, I can't cd to /data. Does the adb server run as a different use in those two cases?Shizukoshizuoka
You don't have access to the /data folder on a real phone. It's chmoded 700. You need root privileges to see it.Fillender
Just in case if you like to obtain the path from the app, it's context.getDatabasePath("<your-database-name>"). Which in fact returns the path above. You can access this path for read-write but only from application that created the database.Korry
Default path where Android saves databases can not be accesed on non-rooted devices. So, the easiest way to access to database file (only for debugging environments) is to modify the constructor of the class. Some answers after this one (exactly here: https://mcmap.net/q/203016/-location-of-sqlite-database-on-the-device), you will find how I managed this.Incapacitate
See here:programatically get the database pathMystic
S
45

For this, what I did is

File f=new File("/data/data/your.app.package/databases/your_db.db3");
FileInputStream fis=null;
FileOutputStream fos=null;

try
{
  fis=new FileInputStream(f);
  fos=new FileOutputStream("/mnt/sdcard/db_dump.db");
  while(true)
  {
    int i=fis.read();
    if(i!=-1)
    {fos.write(i);}
    else
    {break;}
  }
  fos.flush();
  Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
  e.printStackTrace();
  Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
}
finally
{
  try
  {
    fos.close();
    fis.close();
  }
  catch(IOException ioe)
  {}
}

And to do this, your app must have permission to access SD card, add following setting to your manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Not a brilliant way, but works.

Shower answered 31/10, 2012 at 8:55 Comment(4)
Nice. I was looking for this. One could also choose to upload the complete db to a server for analysis this way.Gotland
if it does not work ...use the database name "/data/data/your.app.package/databases/your_db" Simply remove the extension ".db3"Harken
This solution works fine. But, you can use getDatabasePath(your_db_name) to get File object and use getAbsolutePath() to get accurate path instead of constructing it. The getDatabasePath method is defined in ContextWrapper.Cruciferous
File and path names are case sensitive in Android.Ninefold
H
33

The Context contains many path functions: Context.getXXXPath()
One of them is android.content.Context.getDatabasePath(String dbname) that returns the absolute path of a database called dbname.

Context ctx = this; // for Activity, or Service. Otherwise simply get the context.
String dbname = "mydb.db";
Path dbpath = ctx.getDatabasePath(dbname);

The returned path, in this case, would be something like:

/data/data/com.me.myapp/databases/mydb.db

Note that this path is autogenerated if using SQLiteOpenHelper to open the DB.

Handspike answered 10/7, 2014 at 12:1 Comment(0)
M
25

If you're talking about real device /data/data/<application-package-name> is unaccessible. You must have root rights...

Macswan answered 15/12, 2010 at 19:33 Comment(5)
That's not exactly true. It's not browsable, but the accessibility of files within depends on their individual permission bits.Magistracy
My phone is rooted. How can I "browse" the data folder?Obstacle
@SreecharanDesabattula, you need to go to adb shell and switch to superuser using the "su" command to browse the directory.Ensoul
/system/bin/sh: su: not foundHalves
@Sunnyday Try sudo suShericesheridan
I
20

This is and old question, but answering may help others.

Default path where Android saves databases can not be accesed on non-rooted devices. So, the easiest way to access to database file (only for debugging environments) is to modify the constructor of the class:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "/mnt/sdcard/database_name.db", null, 0);
    }
}

Remember to change for production environments with these lines:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    MySQLiteOpenHelper(Context context) {
        super(context, "database_name.db", null, 0);
    }
}
Incapacitate answered 17/2, 2014 at 15:16 Comment(3)
I tried this and returned the error Failed to open database '/mnt/sdcard/itens'. in LogcatRiha
I'm so sorry. This answer is four years old. And the OP is older than that (2010). At this time, Android is significantly different from 2010. So the database location can be in another path. I'm sorry I can't be of more help to you.Incapacitate
Possibly the error occurred because the app was not allowed to write, thanks anywayRiha
K
13

/data/data/packagename/databases/

ie

/data/data/com.example.program/databases/

Kattie answered 15/12, 2010 at 16:50 Comment(0)
R
11

A SQLite database is just a file. You can take that file, move it around, and even copy it to another system (for example, from your phone to your workstation), and it will work fine. Android stores the file in the /data/data/packagename/databases/ directory. You can use the adb command or the File Explorer view in Eclipse (Window > Show View > Other... > Android > File Explorer) to view, move, or delete it.

Reachmedown answered 16/12, 2010 at 12:47 Comment(0)
P
9

well this might be late but it will help. You can access the database without rooting your device through adb

start the adb using cmd and type the following commands

-run-as com.your.package  
-shell@android:/data/data/com.your.package $ ls  
cache  
databases  
lib  
shared_prefs  

Now you can open from here on.

Planimetry answered 11/12, 2013 at 12:36 Comment(1)
Yes, for a debug APK. No, for a production one (or on Android releases where run-as is broken).Magistracy
D
5

If you name your db as a file without giving a path then most common way to get its folder is like:

final File dbFile = new File(getFilesDir().getParent()+"/databases/"+DBAdapter.DATABASE_NAME);

where DBAdapter.DATABASE_NAME is just a String like "mydatabase.db".
Context.getFilesDir() returns path for app's files like: /data/data/<your.app.packagename>/files/ thats why you need to .getParent()+"/databases/", to remove "files" and add "databases" instead.
BTW Eclipse will warn you about hardcoded "data/data/" string but not in this case.

Dunlap answered 7/7, 2014 at 18:17 Comment(0)
S
3

You can access it using adb shell how-to

Content from above link:

Tutorial : How to access a Android database by using a command line. When your start dealing with a database in your program, it is really important and useful to be able to access it directly, outside your program, to check what the program has just done, and to debug.

And it is important also on Android.

Here is how to do that :

1) Launch the emulator (or connect your real device to your PC ). I usually launch one of my program from Eclipse for this. 2) Launch a command prompt in the android tools directory. 3) type adb shell. This will launch an unix shell on your emulator / connected device. 4) go to the directory where your database is : cd data/data here you have the list of all the applications on your device Go in your application directory ( beware, Unix is case sensitive !! ) cd com.alocaly.LetterGame and descend in your databases directory : cd databases Here you can find all your databases. In my case, there is only one ( now ) : SCORE_DB 5) Launch sqlite on the database you want to check / change : sqlite3 SCORE_DB From here, you can check what tables are present : .tables 6) enter any SQL instruction you want : select * from Score;

This is quite simple, but every time I need it, I don't know where to find it.

Shortlived answered 17/11, 2012 at 21:45 Comment(0)
M
3
By Default it stores to:    

String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;

Where:

String DATABASE_NAME = "your_dbname";
String PACKAGE_NAME = "com.example.your_app_name";

And check whether your database is stored to Device Storage. If So, You have to use permission in Manifest.xml :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Morbihan answered 21/2, 2017 at 5:47 Comment(0)
S
2

Do not hardcode path like //data/data/<Your-Application-Package-Name>/databases/<your-database-name>. Well it does work in most cases, but this one is not working in devices where device can support multiple users. The path can be like //data/user/10/<Your-Application-Package-Name>/databases/<your-database-name>. Possible solution to this is using context.getDatabasePath(<your-database-name>).

Steinman answered 6/12, 2019 at 11:25 Comment(0)
Q
1

If your application creates a database, this database is by default saved in the directory DATA/data/APP_NAME/databases/FILENAME.

The parts of the above directory are constructed based on the following rules. DATA is the path which the Environment.getDataDirectory() method returns. APP_NAME is your application name. FILENAME is the name you specify in your application code for the database.

Quinquepartite answered 5/6, 2014 at 6:3 Comment(0)
I
1

You can find your database file :

Context.getDatabasePath(getDatabaseName())

getDatabaseName from class SQLiteOpenHelper

Indifferent answered 28/12, 2020 at 7:14 Comment(0)
S
1

In kotlin you can find it in this way:

    val data: File = Environment.getDataDirectory()
    val currentDBPath = "//data//$packageName//databases//"
    val destDir = File(data, currentDBPath)
Scholium answered 24/3, 2021 at 7:1 Comment(0)
H
1

@Shardul's answer is correct.

Besides that, new Android Studio has a tool called: App Inspection, which you can view database directly.

Here is the version of Android Studio:

Android Studio Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021

enter image description here

enter image description here

Honk answered 15/11, 2021 at 9:4 Comment(0)
D
0

You can also check whether your IDE has a utility like Eclipse's DDMS perspective which allows you to browse through the directory and/or copy files to and from the Emulator or a rooted device.

Donkey answered 6/2, 2013 at 13:38 Comment(0)
S
0

Define your database name like :

private static final String DATABASE_NAME = "/mnt/sdcard/hri_database.db";

And you can see your database in :

storage/sdcard0/yourdatabasename.db
Superpatriot answered 23/5, 2014 at 9:16 Comment(0)
Q
0

public class MySQLiteOpenHelper extends SQLiteOpenHelper { MySQLiteOpenHelper(Context context) { super(context, "/mnt/sdcard/database_name.db", null, 0); } }

Do not hardcode "/sdcard/"; use Environment.getExternalStorageDirectory().getPath() instead

Quartering answered 28/2, 2017 at 2:18 Comment(0)
L
0

don't use hardcoded strings at all.
as many devices have used custom dir.

e.g. String DATABASE_PATH = "/data/data/" + packageName + "/databases/" + dbName;

in latest versions

String dbpath = getApplicationContext().getDatabasePath("dbname").getAbsolutePath() + File.separator;

if u specifically want to get internal path "data/data/..." in latest android versions

String destPath = getApplicationContext().getFilesDir().getPath();
destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases";
Lovel answered 2/4, 2023 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.