简体   繁体   English

在 Android Studio 中查看设备上的 SQLite 数据库

[英]View SQLite database on device in Android Studio

I am using the latest version of Android Studio.我正在使用最新版本的 Android Studio。 When I run my app on the emulator, I am able to view my database by going through:当我在模拟器上运行我的应用程序时,我可以通过以下方式查看我的数据库:

tools -> Android Device Monitor -> clicking on the emulator in the left panel -> file explorer -> data -> data -> com.project-name

But this option isn't available when running my app on a device.但是在设备上运行我的应用程序时,此选项不可用。

I have checked related questions:我检查了相关问题:

  1. Android - viewing SQLite databases on device? Android - 在设备上查看 SQLite 数据库? The most voted answer here suggests copying the database to SDcard, and it's even for eclipse.此处投票最多的答案建议将数据库复制到 SD 卡,它甚至适用于 Eclipse。
  2. Access sqlite database on android device 在安卓设备上访问sqlite数据库

and these questions are from 2011 and 2010. Are there any plugins I can use or other external tools?这些问题来自 2011 年和 2010 年。我可以使用任何插件或其他外部工具吗?

Connect to Sqlite3 via ADB Shell通过 ADB Shell 连接到 Sqlite3

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.我还没有在 Android Studio 中找到任何方法来做到这一点,但我使用远程 shell 访问数据库,而不是每次都提取文件。

Find all info here: http://developer.android.com/tools/help/adb.html#sqlite在此处查找所有信息: http : //developer.android.com/tools/help/adb.html#sqlite

1- Go to your platform-tools folder in a command prompt 1- 在命令提示符中转到您的平台工具文件夹

2- Enter the command adb devices to get the list of your devices 2- 输入命令adb devices以获取adb 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: 3- 将外壳连接到您的设备:

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

4- Navigate to the folder containing your db file: 4- 导航到包含您的 db 文件的文件夹:

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

5- run sqlite3 to connect to your db: 5- 运行 sqlite3 连接到您的数据库:

sqlite3 <your-db-name>.db

6- run sqlite3 commands that you like eg: 6- 运行您喜欢的 sqlite3 命令,例如:

Select * from table1 where ...;

Note: Find more commands to run below.注意:在下面找到更多要运行的命令。

SQLite cheatsheet SQLite 备忘单

There are a few steps to see the tables in an SQLite database:查看 SQLite 数据库中的表有几个步骤:

  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:列出所有可用的 SQLite 提示命令:

     .help

Source : This SO answer..来源:这个SO答案..

The easiest way to see realtime Database are:查看实时数据库的最简单方法是:

#For Android Studio 4.1 Canary 6 and higher #对于Android Studio 4.1 Canary 6及更高版本

You can use a very simple Android Studio's feature Database Inspector .您可以使用一个非常简单的 Android Studio 功能Database Inspector Where you can inspect, query, and modify your app's databases using the new Database Inspector.您可以在其中使用新的数据库检查器检查、查询和修改应用程序的数据库。 For example, you can debug your running app by modifying values in your database and testing those changes on the device in real-time without leaving Android Studio .例如,您可以通过修改数据库中的值并在设备上实时测试这些更改来调试正在运行的应用程序,而无需离开 Android Studio

To get started, deploy your app to a device running API level 26 or higher and select View > Tool Windows > Database Inspector from the menu bar.首先,将您的应用程序部署到运行 API 级别 26 或更高级别的设备,然后从菜单栏中选择“查看”>“工具窗口”>“数据库检查器”。

#For Android Studio 4.0 and lesser #对于Android Studio 4.0 及更低版本

Use the Android Debug Database library使用Android 调试数据库

在此处输入图片说明

  • Android Debug Database allows you to edit , delete , create the database and shared preferences values directly in your browser in a very simple way Android Debug Database 允许您以非常简单的方式直接在浏览器中编辑删除创建数据库共享首选项值。
  • Search in your data在您的数据中搜索
  • Debug Room in-memory database调试室内存数据库
  • No need to root device无需root设备

Using:使用:


Add this to your app's build.gradle将此添加到您的应用程序的 build.gradle

debugImplementation 'com.amitshekhar.android:debug-db:1.0.6'

Launch/Run app启动/运行应用


Find the debugging link in logs in LogCat在 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 any browser链接会有所不同并在任何浏览器中打开它

That's it Enjoy!!!就是这样享受!!!

  • If you are using it over USB, run ADB forward tcp:8080 tcp:8080如果您通过 USB 使用它,请运行ADB forward tcp:8080 tcp:8080
  • Android phone and laptop should be connected to the same Network Android 手机和笔记本电脑应该连接到同一个网络

For more details go to the library .有关更多详细信息,请访问图书馆

Below is the simplest solution for viewing sqlite database in Android.下面是在Android中查看sqlite数据库的最简单的解决方案。

https://github.com/facebook/stetho https://github.com/facebook/stetho

Steps to view SQLite DB查看 SQLite DB 的步骤

1) Add gradle dependency 1)添加gradle依赖

compile 'com.facebook.stetho:stetho:1.5.0' 
implementation 'com.facebook.stetho:stetho:1.5.0' - for kotlin

2) In Application class 2)在应用类

public class MyApplication extends Application {
  public void onCreate() {
   super.onCreate();
   Stetho.initializeWithDefaults(this);
  }
} 

3) Go to google chrome and type ' chrome://inspect/#devices ' to see connected devices and can access SQlite DB. 3) 转到谷歌浏览器并输入“ chrome://inspect/#devices ”以查看连接的设备并可以访问 SQlite DB。

There's a new native way on Android Studio 4.1 version called Database Inspector, check this answer! Android Studio 4.1 版本中有一种新的本地方式,称为 Database Inspector,请查看答案!

在此处输入图片说明

It will be shown in the bottom of android studio它将显示在android studio的底部在此处输入图片说明

This app is very useful, free and will remain free(Confirmed by the developer). 这个应用程序非常有用,免费并且将保持免费(由开发者确认)。 Very easy to add, edit and delete.非常容易添加、编辑和删除。

It will work for rooted device and emulator.它适用于有根设备和模拟器。 Also if you have DB file then you can browse it from SDCard of all devices including non-rooted.此外,如果您有 DB 文件,那么您可以从所有设备(包括非 root 用户)的 SDCard 中浏览它。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM