0

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.

Community
  • 1
  • 1
Karlom
  • 13,323
  • 27
  • 72
  • 116
  • 1
    You could just make the database on your local machine, then copy it to your Android Studio project project. https://github.com/jgilfelt/android-sqlite-asset-helper – OneCricketeer May 21 '16 at 14:22

3 Answers3

2

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.

Daniel
  • 2,355
  • 9
  • 23
  • 30
  • Yep, I was searching the db on my local dev machine! How can I get to the adb terminal though? – Karlom May 21 '16 at 14:59
  • 1
    ADB is located in your `~/SDK/platform-tools/adb` folder. You can find that out exactly where that is if you go to your SDK manager and see where your SDK is installed. Once you're there you can execute the commands. – Daniel May 21 '16 at 15:13
  • When I execute in terminal: `$ ~/Android/Sdk/platform-tools/adb -s emulator-5554 shell pull /data/data/myapp/databases/myapp.db` I get `/system/bin/sh: pull: not found` Any ideas why? – Karlom May 22 '16 at 20:56
  • @Karlom, yes I noticed my commands were wrong, please check updated answer. – Daniel May 22 '16 at 21:00
  • Well, now I get `remote object '/data/data/myapp/databases/myapp.db' does not exist` when I try to pull it. How to check the databases on the path? Is there something like `ls` command for adb? – Karlom May 22 '16 at 21:03
  • 1
    @Karlom, the app name is the full qualified java app name. Here is an example: `com.danielkaparunakis.stackoverflowquestions`. So if I wanted to pull the database for this app, I would type: `$ ~/Android/Sdk/platform-tools/adb -s emulator-5554 pull /data/data/com.danielkaparunakis.stackoverflowquestions/databases/myapp.db` – Daniel May 22 '16 at 21:11
  • Yes Daniel. Please complete your answer with additional details and I'll accept it, as you had the most complete answer. Many thanks. – Karlom May 22 '16 at 21:16
  • 1
    @Karlom Be aware that this methods works on emulators, not on devices unless they are rooted – which I don't think is a good idea if just to pull/push databases. – totoro May 23 '16 at 10:02
1

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

John Roberts
  • 467
  • 3
  • 5
1

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
totoro
  • 2,469
  • 2
  • 19
  • 23
  • Where should I execute this command? I'm very new to android and don't know how to get to the studio's terminal. – Karlom May 21 '16 at 15:07
  • I execute it from my shell (bash). `adb` has to be in your path. – totoro May 21 '16 at 15:07
  • If you mean, the linux machine's terminal, it yeild: `me@pc:~$ LC_ALL=C adb exec-out run-as $package cat databases/$db >db.sqlite The program 'adb' is currently not installed. To run 'adb' please ask your administrator to install the package 'android-tools-adb'`, and there is not such package on ubuntu repository. – Karlom May 21 '16 at 15:09
  • Add the folder of `adb` to your path or just run it with full path. You need to find at first though: `locate adb`. – totoro May 21 '16 at 15:11
  • 1
    Normally you can find it at `~/Android/Sdk/platform-tools/adb`. – totoro May 21 '16 at 15:13
  • Alright I could dump the `db.sqlite` but how can I upload it back to the device when I batch inserted the data? – Karlom May 21 '16 at 15:26
  • So far so good, you could take a look at http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ if you wish to keep the app's database on your system. – totoro May 21 '16 at 15:31