View contents of database file in Android Studio

僤鯓⒐⒋嵵緔 提交于 2019-11-26 01:34:32

问题


I have been using Android Studio to develop my app since it\'s was released.

Everything works nice until recently, I have to debug together with checking the database file. Since I don\'t know how to see the database directly, When I debugged to generate the database file, I have to export the database file from my phone to the PC.

In order to do this, I have to open DDMS > File Explorer. Once I open the DDMS, I have to reconnect the USB, and I lose my debug thread. After checking the database file, I have to close the DDMS and reconnect the USB again to get back to the debug mode.

It\'s just too complicated. Does anyone have a better way to do this in Android Studio (I know it\'s easier in Eclipse) ?


回答1:


Viewing databases from Android Studio:

Edit: To view your database on an Emulator follow these steps (for actual device, scroll to the bottom):

  1. Download and install SQLiteBrowser.
  2. Copy the database from the device to your PC:

    • Android Studio versions < 3.0:

      • Open DDMS via Tools > Android > Android Device Monitor

      • Click on your device on the left.
        You should see your application:

      • Go to File Explorer (one of the tabs on the right), go to /data/data/databases

      • Select the database by just clicking on it.

      • Go to the top right corner of the Android Device Monitor window. Click on the 'pull a file from the device' button:

      • A window will open asking you where you want to save your database file. Save it anywhere you want on your PC.

    • Android Studio versions >= 3.0:

      • Open Device File Explorer via View > Tool Windows > Device File Explorer

      • Go to data > data > PACKAGE_NAME > database, where PACKAGE_NAME is the name of your package (it is com.Movie in the example above)

      • Right click on the database and select Save As.... Save it anywhere you want on your PC.

  3. Now, open the SQLiteBrowser you installed. Click on 'open database', navigate to the location you saved the database file, and open. You can now view the contents of your database.


To view your database on your mobile device:

Go to this Github repository and follow the instructions in the readme to be able to view your database on your device. What you get is something like this:

That's it. It goes without saying however that you should undo all these steps before publishing your app.




回答2:


Connect to Sqlite3 via ADB Shell

I haven't found any way to do that in Android Studio, but I access the db with a remote shell instead of pulling the file each time.

Find all info here: http://developer.android.com/tools/help/adb.html#sqlite

1- Go to your platform-tools folder in a command prompt

2- Enter the command adb devices to get the list of your devices

C:\Android\adt-bundle-windows-x86_64\sdk\platform-tools>adb devices
List of devices attached
emulator-xxxx   device

3- Connect a shell to your device:

C:\Android\adt-bundle-windows-x86_64\sdk\platform-tools>adb -s emulator-xxxx shell

4- Navigate to the folder containing your db file:

cd data/data/<your-package-name>/databases/

5- run sqlite3 to connect to your db:

sqlite3 <your-db-name>.db

6- run sqlite3 commands that you like eg:

Select * from table1 where ...;

Note: Find more commands to run below.

SQLite cheatsheet

There are a few steps to see the tables in an SQLite database:

  1. List the tables in your database:

    .tables
    
  2. List how the table looks:

    .schema tablename
    
  3. Print the entire table:

    SELECT * FROM tablename;
    
  4. List all of the available SQLite prompt commands:

    .help
    



回答3:


I'm actually very surprised that no one has given this solution:

Take a look at Stetho.

I've used Stetho on several occasions for different purposes (one of them being database inspection). On the actual website, they also talk about features for network inspection and looking through the view hierarchy.

It only requires a little setup: 1 gradle dependency addition (which you can comment out for production builds), a few lines of code to instantiate Stetho, and a Chrome browser (because it uses Chrome devtools for everything).

Update: You can now use Stetho to view Realm files (if you're using Realm instead of an SQLite DB): https://github.com/uPhyca/stetho-realm

Update #2: You can now use Stetho to view Couchbase documents: https://github.com/RobotPajamas/Stetho-Couchbase

Update #3: Facebook is focusing efforts on adding all Stetho features into its new tool, Flipper. Flipper already has many of the features that Stetho has. So, now may be a good time to make the switch. https://fbflipper.com/docs/stetho.html




回答4:


Simplest method when not using an emulator

$ adb shell
$ run-as your.package.name
$ chmod 777 databases
$ chmod 777 databases/database_name
$ exit
$ cp /data/data/your.package.name/databases/database_name /sdcard
$ run-as your.package.name # Optional
$ chmod 660 databases/database_name # Optional
$ chmod 660 databases # Optional
$ exit # Optional
$ exit
$ adb pull /sdcard/database_name

Caveats:

I haven't tested this in a while. It may not work on API>=25. If the cp command isn't working for you try one of the following instead:

# Pick a writeable directory <dir> other than /sdcard
$ cp /data/data/your.package.name/databases/database_name <dir>

# Exit and pull from the terminal on your PC
$ exit
$ adb pull /data/data/your.package.name/databases/database_name

Explanation:

The first block configures the permissions of your database to be readable. This leverages run-as which allows you to impersonate your package's user to make the change.

$ adb shell
$ run-as your.package.name
$ chmod 777 databases
$ chmod 777 databases/database_name
$ exit # Closes the shell started with run-as

Next we copy the database to a world readable/writeable directory. This allows the adb pull user access.

$ cp /data/data/your.package.name/databases/database_name /sdcard

Then, replace the existing read/write privileges. This is important for the security of your app, however the privileges will be replaced on the next install.

$ run-as your.package.name
$ chmod 660 databases/database_name 
$ chmod 660 databases
$ exit # Exit the shell started with run-as

Finally, copy the database to the local disk.

$ exit # Exits shell on the mobile device (from adb shell) 
$ adb pull /sdcard/database_name



回答5:


In Android Studio 3 and above You Can See a "Device File Explorer" Section in Right-Bottom Side of Android Studio.

Open it, Then You Can See The File Tree, You Can Find an Application Databases In this Path:

/data/data/{package_name}/databases/




回答6:


Finally, i found a simplest solution which do not need to open the DDMS.

Actually, the solution is based on what @Distwo mentioned, but it don't have to be that complicated.

First, remember the path of your database file in the device, it should aways the same. For example mine is:/data/data/com.XXX.module/databases/com.XXX.module.database

Second, execute this command which pulls your database file onto you PC

 adb pull /data/data/com.XXX.module/databases/com.XXX.module.database /Users/somePathOnYourPC/

What you need to do is just copy and store this command, then you can use it again and again.

Third, if you got Permission Denied or something like that, just run adb root before the previous command.




回答7:


Try this android studio plugin SQLScout. you can see and edit your app database realtime.

Edit: Remember it's a paid plugin, but have 24h trial and suitable for headache time.




回答8:


After going through all the solutions i will suggest

Use Stethos
Lets see how Simple it is

Add following dependencies in build.gradle file

compile 'com.facebook.stetho:stetho:1.5.0'
compile 'com.facebook.stetho:stetho-js-rhino:1.4.2'

Then go to you mainActivity onCreate method add following line

Stetho.initializeWithDefaults(this);

this would require you to

import com.facebook.stetho.Stetho;

now while running app from android studio , Open Chrome and in address bar type
chrome://inspect/

in the resources tab >web SQL check the the database and table play with it in real time easily




回答9:


To know where sqlite database stored created by you in android studio, you need to follow simple steps:

1.Run your application
2.Go to Tools--->Android---->Device Monitor
3.Find your application name in left panel
4.Then click on File Explorer tab
5.Select data folder
6.Select data folder again and find your app or module name
7.Click on your  database name
8.On right-top window you have an option to pull file from device.
9.Click it and save it on your PC
10.Use FireFox Sqlite manager to attach it to your project.

For more information this link will be useful. http://www.c-sharpcorner.com/UploadFile/e14021/know-where-database-is-stored-in-android-studio/

To view your data present in db file you need to download sqlite browser or add plugin of the same in any browser so you need to open file in browser and view your data.

Download browser from http://sqlitebrowser.org/

Here is screenshot showing browser containing register database

Thanks,




回答10:


The simplest way is to Attach your device and run Android Studio Then From Tool Bar:

  1. View --> Tools Window --> Device File Explorer
  2. Go into data/data and find your package
  3. find the DB file you want to explore and download it
  4. I recommend on this online tool: https://sqliteonline.com/ to explore the db file

Another way is using Stetho library:

  1. Add Stello dependency to your build.gradle:

    compile 'com.facebook.stetho:stetho:1.5.0'

  2. Put Following line on your onCreate() of your Application class or main Activity:

    Stetho.initializeWithDefaults(this);

  3. Connect your device, run the app and Enter the following site on Chrome:

    chrome://inspect/#devices

and that is it. you can now explore your tables.

Note: it is recommended to remove the dependency before moving to production.




回答11:


If you want to browse your databases FROM WITHIN ANDROID STUDIO, here's what I'm using:

Go to Files / Settings / Plugins and look for this:

... After having restarted Android Studio you can pick your downloaded database file like this:

... Click the "Open SQL Console" icon, and you end up with this nice view of your database inside Android Studio:




回答12:


I know the question is rather old but I believe this issue is still present.

Viewing databases from your browser

I created a development tool that you can integrate as a lib into your android app project. The tool opens a server socket in your app to communicate via web browser. You can browse through your whole database and download the database file directly through the browser.

Integration can be done via jitpack.io:

project build.gradle:

//...
allprojects {
    repositories {
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}
//...

app build.gradle:

//...
dependencies {
    //...
    debugCompile 'com.github.sanidgmbh:debugghost:v1.1'
    //...
}
//...

Setup Application class

In order to only compile DebugGhostLib in certain build-types or product-flavours we need an abstract Application class which will be derived in the special flavours. Put the following class in your main folder (under java > your.app.package):

public class AbstractDebugGhostExampleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Do your general application stuff
    }
}

Now, for your release build type (or product flavour), you add the following Application class to your release (or product-flavour) folder (also under java > your.app.package):

public class MyApp extends AbstractDebugGhostExampleApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

This is the application class that will not reference DebugGhostLib.

Also tell your AndroidManifest.xml that you're using your own application class. This will be done in your main folder:

<manifest package="demo.app.android.sanid.com.debugghostexample" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- permissions go here -->
    <application android:name=".MyApp"> <!-- register your own application class -->
        <!-- your activities go here -->
    </application>
</manifest>

Now, for your debug build type (or product flavour), you add the following Application class to your debug (or product-flavour) folder (also under java > your.app.package):

public class MyApp extends AbstractDebugGhostExampleApplication {
    private DebugGhostBridge mDebugGhostBridge;

    @Override
    public void onCreate() {
        super.onCreate();

        mDebugGhostBridge = new DebugGhostBridge(this, MyDatabaseHelper.DATABASE_NAME, MyDatabaseHelper.DATABASE_VERSION);
        mDebugGhostBridge.startDebugGhost();
    }
}

You can get the tool here.




回答13:


Open the Device File Explore Terminal at the Bottom of the Android Studio.

Open The folder named Data , then inside Data again open the Folder Data .

Scroll Down the list of folders and find the folder with your.package.name. Open folder your.package.name > Database. You get your.databaseName. Right Click your.databaseName and Save as to C:/your/Computer/Directory.

Go to C:/your/Computer/Directory open your.databaseName with DB SQLite




回答14:


For Android Studio 3.X

  1. View -> Tool Windows -> Device File Explorer
  2. In The File Explorer data->data->com.(yourapplication package)->databases
  3. Right Click On The Database And Save on your local machine. To open, the file you can use SQLite Studio by just dragging the Database file on it.

Here is the link to SQLite Studio : https://sqlitestudio.pl/index.rvt?act=download




回答15:


Simple and easy way to see database content within Android studio.

First, install Database Navigator plugin in Android Studio

Second, Restart Android Studio

Third, save the database into the default location like: (C:\Users\User Name\Documents\AndroidStudio\DeviceExplorer\emulator or device\data\data\package name\databases)

Fourth, Connect saved dbname_db file in Database Navigator

Give same DB file path that is used in step Third

i.e (C:\Users\User Name\Documents\AndroidStudio\DeviceExplorer\emulator or device\data\data\package name\databases\package naem\dbname_db)

Finally, just test DB connection and open console and do whatever you want to do.

If you want to refresh DB just repeat step Second and just save it or refresh.

HAPPY CODING!!!!!




回答16:


This is a VERY old question and my answer is similar to some answers above but done MUCH faster. The script below is for Mac but I'm sure someone can modify it for Windows.

1) Open Script Editor on your Mac (you can just search for Script Editor in Spotlight)
2) Copy and paste the text below and modify it with your SDK path, package name, etc. (see below)
3) Save the script!!

Thats's it! Just press the play button on top to get the updated database file, which will be on your desktop.

replace the following things in the script below:

path_to_my_sdk ==>> put full path to your sdk
my_package_name ==>> package name of your application
myDbName.db ==>> file name of your database

set getLocalDb to "path_to_my_sdk/platform-tools/adb shell run-as my_package_name chmod 777 databases && path_to_my_sdk/platform-tools/adb shell run-as my_package_name chmod 777 databases/myDbName.db && path_to_my_sdk/platform-tools/adb shell run-as my_package_name cp databases/myDbName.db /sdcard/myDbName.db && path_to_my_sdk/platform-tools/adb pull /sdcard/myDbName.db /Users/User/Desktop/myDbName.db"
do shell script getLocalDb

Hope this helps someone.




回答17:


I had to pull the database from phone to PC. Now I use this very well made and free app which has a lot of features. It works on API 4.1+with a Rooted device.
Let me know if you find how to make it work without root.

https://play.google.com/store/apps/details?id=com.lastempirestudio.sqliteprime




回答18:


This might not be the answer you're looking for, but I don't have a better way for downloading DB from phone. What I will suggest is make sure you're using this mini-DDMS. It will be super hidden though if you don't click the very small camoflage box at the very bottom left of program. (tried to hover over it otherwise you might miss it.)

Also the drop down that says no filters (top right). It literally has a ton of different ways you can monitor different process/apps by PPID, name, and a lot more. I've always used this to monitor phone, but keep in mind I'm not doing the type of dev work that needs to be 120% positive the database isn't doing something out of the ordinary.

Hope it helps




回答19:


I've put together a unix command-line automation of this process and put the code here:

https://github.com/elliptic1/Android-Sqlite3-Monitor

It's a shell script that takes the package name and database name as parameters, downloads the database file from the attached Android device, and runs the custom script against the downloaded file. Then, with a unix tool like 'watch', you can have a terminal window open with a periodically updating view of your database script output.




回答20:


I'm using windows 7, my device is a emulated android device API 23. I suppose it is the same for any real device as long as it is rooted and the API is not above 23

Go to Tools -> Android -> Android Device Monitor. Go to File Explorer. In my case, it is in data/data//app_webview/databases/file_0/1

I have to manually add .db at the end of the file named "1"




回答21:


Its a combination from other answers with a plus

  1. Install DB Browser for SQLite
  2. Get the exact location from Device File Explorer for MyBase.db something like "/data/data/com.whatever.myapp/databases/MyBase.db"
  3. Set the project window from Android to Project - so you can see the files (build.gradle, gradle.properties,...etc)
  4. In Project window right click and chose Open in Terminal
  5. In terminal you are now in the root director of your application from the hardisk so execute: adb pull /data/data/com.whatever.myapp/databases/MyBase.db
  6. Now on the Project window of you Android Studio you have a file "MyBase.db"
  7. Double click your db file from Project and you can now browse/edit your databases in DB Browser
  8. When you are ready just do Write Changes in DB Browser so databases are saved on hardisk
  9. In terminal with the command: adb push MyBase.db /data/data/com.whatever.myapp/databases/MyBase.db you just send from the Hardisk to device the edited database.



回答22:


The easiest way is to use Android Debug Database library (6.7k stars on GitHub).

Advantages:

  • Fast implementation
  • See all the databases and shared preferences
  • Directly edit, delete, create the database values
  • Run any SQLite query on the given database
  • Search in your data
  • Download database
  • No need to root device
  • Adding custom database files

How to use:

  1. Add debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' to build.gradle (module)
  2. Launch app
  3. Find the debugging link in logs (in LogCat) (i.e. D/DebugDB: Open http://192.168.232.2:8080 in your browser, the link will be different) and open it in the browser
  4. Enjoy the powerful debugging tool

Important:

  • Your Android phone and laptop should be connected to the same Network (Wifi or LAN)
  • If you are using it over USB, run adb forward tcp:8080 tcp:8080

For more information go to the library page on GitHub.



来源:https://stackoverflow.com/questions/17529766/view-contents-of-database-file-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!