How to find database file generated of my application in android device - android

I made an application which collects information of device and create a database using SQLite in android. Using file explorer, i can see database of my application in Emulator using Eclipse. I attached the image as shown.
I want to collect information of my android device. I execute the application in my device. Now i am not able to find neither package name of my application nor database of my application.
Please help?

You will only be able to access the database on a physical device if your device is rooted. An emulator gives full access to the databases, where as a device does not, unless it is rooted.

You can try shelling into the device from the command line over USB like so:
# normal shell on local machine
adb shell
# android shell on device
run-as [your.package.name.here]
cd databases
sqlite3 [database_name]
Note that on some devices you do not have access to sqlite3 from the shell like this. In those cases I have copied the database file to the external storage directory (usually /sdcard), exited the shell, then used adb pull to retrieve it.
# android shell on device
cp [database_name] /sdcard
exit
# normal shell on local machine
cd [somewhere]
adb pull /sdcard/[database_name] .

Related

Is it possible to pull and inspect the database file of contacts in android

I am using android Pie, tring to understand contentprovider in the context of contacts table, it is a bit confusing so I am trying to figure out how to pull the contacts database file from android's storage, so that I can inspect the structure and contents of the table.
Please advise. did some research and it is apparently supposed to be here
/data /data /com.android.providers.contacts /databases /contacts2.db
Using android studio when I browse this location it does not show any contents please advise
Yes, it is possible, although you may need a device that allows root access. The path you posted looks correct, but you connect to your device via ADB and search for it on the device:
$ adb root
$ adb remount
$ adb shell
$ find . -name "contacts2.db"
Copy the path, and then exit out of adb and use:
$ adb pull <path>
You will then be able to access the file with a SQL database browser.

Is there anyway to get database of non debuggable android application on non-rooted device with adb?

I need to get my android application database with usb connection on non-rooted device and I want to use adb commands.
I've used ==> "adb exec-out run-as mypackagename cat /databases/mydbname.db > myPCaddress(D:\mydbname.db)" not working and for adb pull I don't have permission.
So one of the solutions is to copy the database out of the data directory to some other location, let's say an sd card.
1) Create a folder in an accessible place that you will copy the database to: mkdir /sdcard/data/
2) run-as <com.your.package> (which will lead you to your package's data directory, or you can just use cd to navigate there I think)
3) cp databases/<database>.db /sdcard/data/ (copy the database file)
4) exit all the way out
And once you copied the file you can either navigate there (to /sdcard/data/) and use sqlite3 to navigate through it. Or you can use adb pull because it's in an accessible location. You can find the source at this.

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.
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.
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
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

USB debugging how to get database file?

Hi I know that when we are using emulator for run an App in android we are able to get the database and shared preference file from File explorer. Like wise is there any method to obtain the database and shared preference from device when we are testing the App using usb debugging.
Any help will be appreciable
Assuming your device is not rooted.
In this case, you have to make sure the app you want to retrieve is debug signed (not a 3rd party commercial app).
Then there are 2 ways to get the db and other files:
Way 1: See the content inside adb shell
Run below command:
adb shell
run-as [your app package name]
(above command will let you to go to /data/data/[your app package name] folder)
cd databases
sqlite3 [your database file name]
(this will start a sqlite3 prompt, you can do simple db browsing with it)
Way 2: Pull the file to local
Run below command:
adb shell run-as [your app package name] chmod 666 databases/[your databases file name]
adb pull /data/data/[your app package name]/databases/[your database file name]

How to install or get access to sqlite3 from adb shell

I need a way to install or somehow get access to sqlite3 in the adb shell. I have rooted my device.
I've tried to find an answer but the closed I could come is:
Why do I get a "sqlite3: not found" error on a rooted Nexus One when I try to open a database using the adb shell?
But I don't think it's good idea to push my windows sqlite3.exe on a linux system?
So is it possible to install the sqlite3 terminal browser somehow?
[SOLUTION]
From the different comments and some asking around at #android-dev (irc), I found a solution. First I copied the database file to my desktop. But fist I had to install BusyBox, because cp isn't included?!? After that ran I into the problem that I couldn't pull or push from anywhere but /sdcard/ . I could then use /sdcard/ as a "middle station" and pull/push my db.
Then I got exhausted! I really had to have my sqlite terminal explore. Then I got the idea to start the emulator pull the sqlite binary from /system/xbin/sqlite3. Then remount /system with rw:
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
and push sqlite to the /sdcard/, and from there copy it to /system/xbin/
Now it works :D
Download this app from google play will help you install sqlite3 on android https://play.google.com/store/apps/details?id=ptSoft.util.sqlite3forroot
You don't need root to pull the database from your device. Simply run the following commands:
adb shell run-as <package-name> "cp databases/<db_name>.db /sdcard/ && exit"
adb pull /sdcard/<db_name>.db ~/Downloads/
From there, you can use sqlite3 for whatever operating system you're using (http://www.sqlite.org/download.html), or a sqlite browser such as "DB Browser for SQLite" (http://sqlitebrowser.org/)
I use Rajath's technique... Adb "Pull" the db from the emulator/device, work on it, then adb "push" it back onto/into the emulator device.
also:
I use the free SQLite Editor from the Android Market. I have not rooted my LG Ally and therefor can only edit database tables on my sdcard with SQLite Editor.
Rajath suggests using the adb to push and pull the databases to and from the emulator/device. The work on the database with the windows (or whatever) sqlite3 program you have. He does not suggest pusing the windows sqlite3 onto the Android device, IMHO.
I note that java/android "query()" sends actual SQL commands programmacitacly to ones program with user input. I conclude that sqlite3 is in Android somewhere.
When using the emulator Dev Tools is available, and way down at the bottom of the list is the Terminal Emulator. This allows exploration of file structure of Android in the emulator. However using "adb shell" from the PC has root permissions.
good luck. cactus mitch
You can do this with adb shell without issue.
In terminal or CMD (assuming you have the ADB path set and your phone has ROOT) type:
$ adb shell
$ cd data/data/com.nameofyourpackage/databases/
$ ls to find the name of your database
$ sqlite3 nameofyourdb.db
Then you can use .tables .schema to see the data you need to create the appropriate query.

Categories

Resources