Mukund
Mukund

Reputation: 1105

How to view sqlite database from device file explorer in android studio without saving it to system

In the old eclipse IDE there was an option to install sqlite browser and view the database in the ide itself without downloading and saving it to our system.

In the new android studio there is an option call device explorer where you can go to data/data//databases/yourdb.db

but when you click on it you will be provided with open, save as, delete, synchronize and copypath

when i click on open

enter image description here

but couldn't find anything to load the db in that options. I scrolled to end i checked.

Please help me in finding out what app from that selection should i use to view the db if it have any.

Or can i install some plugins to android studio and view the db directly from devive explorer

One more question is there is no Tools/Android section in new Android studio to view android device monitor where can i find android device monitor in new android studio

Please help

Upvotes: 1

Views: 3970

Answers (2)

Nilesh Deokar
Nilesh Deokar

Reputation: 2985

As of now there is no option to open db in Android Studio however you can try these options :

I believe you are trying to open db in Android studio itself just to save time, if this is the case you can use this Python script to copy db into your local machine and then open in SQLite DB browser.

import sys
import subprocess
import re


#/
# Created by @nieldeokar on 25/05/2018.
#/

# 1. Python script which will copy database file of debuggable apps from the android device to your computer using ADB.
# 2. This script ask for PackageName and DatabaseName at runtime.
# 3. You can make it static by passing -d at as command line argument while running script and setting defaults in following way.
# 4. Edit script and change the values of varialbe packageName and dbName to debuggable app package name and database name then
# run script as : python Copydbfileandroid.py -d 

useDefaults = False


def checkIfPackageInstalled(strSelectedDevice) :

    packageName = 'com.nileshdeokar.healthapp.debug'
    dbName = 'healthapp.db'


    if not useDefaults : 
        print('Please enter package name : ')
        packageName = raw_input()

    packageString = 'package:'+packageName

    try:
        adbCheckIfPackageInstalledOutput = subprocess.check_output('adb -s ' + strSelectedDevice + ' shell pm list packages | grep -x '+ packageString, shell=True)
    except subprocess.CalledProcessError as e:
                print "Package not found"
                return


    if packageString.strip() == adbCheckIfPackageInstalledOutput.strip() : 
        if not useDefaults : 
            print('Please enter db name : ')
            dbName = raw_input()

        adbCopyDbString = 'adb -s '+strSelectedDevice + ' -d shell \"run-as '+packageName+' cat /data/data/'+packageName+'/databases/'+ dbName +'\" > '+dbName

        try:
            copyDbOp = subprocess.check_output(adbCopyDbString,shell=True)
        except subprocess.CalledProcessError as e:
                return

        if "is not debuggable" in copyDbOp :
            print packageString + 'is nto debuggable'

        if copyDbOp.strip() == "":
            print 'Successfully copied '+dbName + ' in current directory'

    else :
        print 'Package is not installed on the device'



defaultString = "-d"
if len(sys.argv[1:]) > 0 and sys.argv[1] == defaultString :
        useDefaults = True

listDevicesOutput = subprocess.check_output("adb devices", shell=True)
listDevicesOutput = listDevicesOutput.replace("List of devices attached"," ").replace("\n","").replace("\t","").replace("\n\n","")

numberofDevices = len(re.findall(r'device+', listDevicesOutput))

connectedDevicesArray = listDevicesOutput.split("device")   
del connectedDevicesArray[-1] 


strSelectedDevice = ''
if(numberofDevices > 1) :
    print('Please select the device : \n'),

    for idx, device in enumerate(connectedDevicesArray):
        print idx+1,device

    selectedDevice = raw_input()

    if selectedDevice.isdigit() :
        intSelected = int(selectedDevice)
        if 1 <= intSelected <= len(connectedDevicesArray) :
            print 'Selected device is : ',connectedDevicesArray[intSelected-1]
            checkIfPackageInstalled(connectedDevicesArray[intSelected-1])
        else :
            print 'Please select in range'
    else : 
        print 'Not valid input'

elif numberofDevices == 1 :
    checkIfPackageInstalled(connectedDevicesArray[0])
elif numberofDevices == 0 :
    print("No device is attached")

Execution : Set default variables like packageName & dbName in script and then

python Copydbfileandroid.py -d

Upvotes: 1

TejpalBh
TejpalBh

Reputation: 437

In AS 3.1.2 there is option to right side as 'Device File Explorer' click on it then select data\data\'package name'\database\ here you will get your database file. Save it on any drive location. To view content install 'DB Browser for SQLite' there is option open database

Upvotes: 2

Related Questions