Karlom
Karlom

Reputation: 14834

Where does android studio save sqlite db on Linux dev machine?

I'd like to insert data directly into the sqlite database of my app but I cannot find it anywhere on android studio path, even on my root path:

$sudo find /  -type f -name 'myapp.db'

I know several similar questions have been asked before but the answers for Windows did not help me on Ubuntu Linux. So appreciate your help.

Upvotes: 0

Views: 952

Answers (3)

totoro
totoro

Reputation: 2456

You app's db is only on the device. You can pull it from any connected device – non-rooted physical devices as well. This script pulls it from the first device.

This trick is run-as <package name> which runs a shell the app's directory with full access to the app's data.

Replace $package with your app's package name and replace $db with the name of you app's db.

$ LC_ALL=C adb exec-out run-as $package cat databases/$db >db.sqlite

LC_ALL=C is to avoid some strange locale behavior on some systems.

adb is by default installed by Android Studio to ~/Android/Sdk/platform-tools/adb.

Update

The program 'adb' is currently not installed. To run 'adb' please ask your administrator to install the package 'android-tools-adb'

This is Ubuntu telling you that you can install it from the Ubuntu package manager.

Normally you would already have it as a part of Android Studio.

Update 2

I don't have a script yet for pushing it back since push and run-as don't work together. You would have to do something like this (untested).

$ adb push db.sqlite /sdcard/temp.sqlite
$ cat <<EOF | adb shell
run-as $package
cat /sdcard/temp.sqlite >databases/$db
exit
exit
EOF

Upvotes: 1

Daniel
Daniel

Reputation: 2373

Android Studio does not store the database locally in your computer. The databases only exist in the devices & every time you deploy to a new device, your database will be created new in that new device. That is why you can't find it in your computer. Here is where the database is located in the device:

/data/data/full_qualified_java_package_name/databases/database_name.db

Now if you would like to insert data directly, you can use the terminal in Android Studio & use ADB to pull the database off the emulator, modify it, and push it back in. Heck I am sure that if you know enough Linux you could probably insert what you need into it without pulling it from the device. Here are some sample commands for the Android Studio terminal for that:

~/Android/Sdk/platform-tools/adb devices

Get the device number, then:

~/Android/Sdk/platform-tools/adb -s emulator-#### pull /data/data/full_qualified_java_package_name/databases/database_name.db <local-filepath>

And to send it back in, it is just:

~/Android/Sdk/platform-tools/adb -s emulator-#### push <local-filepath> /data/data/full_qualified_java_package_name/databases/database_name.db

Example:

~/Android/Sdk/platform-tools/adb -s emulator-5554 pull /data/data/com.danielkaparunakis.stackoverflowquestions/databases/Questiondatabase.db /Users/DanielKaparunakis/Desktop

Additional tip: If you leave the blank when you pull like this:

~/Android/Sdk/platform-tools/adb -s emulator-5554 pull /data/data/com.danielkaparunakis.stackoverflowquestions/databases/Questiondatabase.db

It will automatically pull it to your project's root folder.

Upvotes: 2

John Roberts
John Roberts

Reputation: 487

It will save it in the internal storage of every device, if you don't have a rooted device it will not allow you to pull it, but, if you are using an emulator you will be able to pull it.

https://developer.android.com/training/basics/data-storage/databases.html

Upvotes: 1

Related Questions