自应用发布以来,我一直在使用Android Studio开发应用。

直到最近,一切都很好,我必须调试和检查数据库文件。由于我不知道如何直接看到数据库,当我调试生成数据库文件时,我不得不将数据库文件从我的手机导出到PC上。

为此,我必须打开DDMS >文件资源管理器。一旦我打开DDMS,我必须重新连接USB,我失去了调试线程。在检查数据库文件之后,我必须关闭DDMS并重新连接USB以回到调试模式。

这太复杂了。有人有更好的方法来做到这一点在Android Studio(我知道它更容易在Eclipse) ?


当前回答

通过ADB Shell连接到Sqlite3

我还没有找到任何方法来做到这一点,在Android工作室,但我访问db与远程shell,而不是每次拉动文件。

在这里找到所有信息: http://developer.android.com/tools/help/adb.html#sqlite

1-在命令提示符中进入你的平台工具文件夹

2-输入命令adb devices获取您的设备列表

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

3-连接一个外壳到您的设备:

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

4-导航到包含你的db文件的文件夹:

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

5-运行sqlite3连接到你的数据库:

sqlite3 <your-db-name>.db

6-运行你喜欢的sqlite3命令,例如:

Select * from table1 where ...;

注意:在下面可以找到更多要运行的命令。

SQLite备忘单

在SQLite数据库中查看表有几个步骤:

列出数据库中的表: .tables 列出表格的外观: . schema的表 打印整个表格: SELECT * FROM tablename; 列出所有可用的SQLite提示命令: .help

其他回答

虽然这是一个很老的问题,但我认为它今天仍然有意义。在最新版本的android studio中,我找到了一种方法来查看它(我不确定以前的版本中是否可用)。]

下面是如何查看数据库中的数据:

步骤:

Go to View > Tool Windows > Device File Explorer Then the folder data > data and you will see applications lists. There scroll down a little bit and see the package [ your package for application ]. After finding your package, see databases folder and select file, right click it and save [ download ] As you can't see the data directly, there is a tool, DB Browser for SQLite, go website https://sqlitebrowser.org/ Open Db browser and click open database and choose the database table you downloaded and it will show you the data.

新编辑

现在(当你阅读这篇文章的时候),这甚至更容易了,因为Android工作室已经提供了数据库检查器(就在右下角附近)。

点击它,你会看到数据库。 但是,请确保API的最小级别是26,如果不通过构建更改它。gradle文件作为minSdkVersion 26

简单和容易的方法来查看Android工作室中的数据库内容。

# Android Studio 4.1 Canary 6及更高版本

你可以使用非常简单的Android Studio的数据库检查器功能。 在这里,您可以使用新的数据库检查器检查,查询和修改应用程序的数据库。例如,你可以通过修改数据库中的值来调试正在运行的应用程序,并在不离开Android Studio的情况下在设备上实时测试这些更改。

首先,将应用程序部署到运行API级别26或更高的设备上,并从菜单栏中选择“查看>工具Windows >数据库检查器”。

# Android Studio 4.0及以下版本

首先,在Android Studio中安装数据库导航插件

第二,重启Android Studio

第三,将数据库保存到默认位置,如:(C:\Users\User . 名称\文档\AndroidStudio\DeviceExplorer\模拟器或设备\数据\数据\包名\数据库)

第四,连接数据库导航器中保存的dbname_db文件

给出第三步中使用的相同的DB文件路径

i.e . (C)

最后,只需测试DB连接和打开控制台,做任何你想做的事情。

如果你想要刷新数据库,请重复步骤2并保存或刷新。

编码快乐! !

不使用模拟器时最简单的方法

$ 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

警告:

我有段时间没测试了。API>=25时可能不起作用。 如果cp命令不适合你,试试下面的命令:

# 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

解释:

第一个块将数据库的权限配置为可读。这利用了run-as,它允许您模拟包的用户来进行更改。

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

接下来,我们将数据库复制到一个世界可读/可写目录。这允许adb拉用户访问。

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

然后替换现有的读写权限。这对于您的应用程序的安全性很重要,但是特权将在下次安装时被替换。

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

最后,将数据库复制到本地磁盘。

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

事实上,我很惊讶没有人给出这个解决方案:

看看Stetho。

为了不同的目的(其中之一是数据库检查),我在多个场合使用过Stetho。在实际的网站上,他们还讨论了网络检查和查看视图层次结构的功能。

它只需要一个小小的设置:1个gradle依赖项添加(你可以注释掉生产构建),几行代码来实例化Stetho,和一个Chrome浏览器(因为它使用Chrome devtools来做所有事情)。

更新: 您现在可以使用Stetho查看Realm文件(如果您使用Realm而不是SQLite DB): https://github.com/uPhyca/stetho-realm

更新2: 现在可以使用Stetho查看Couchbase文档:https://github.com/RobotPajamas/Stetho-Couchbase

更新# 3: Facebook正致力于将Stetho的所有功能添加到其新工具Flipper中。Flipper已经具备了Stetho的许多功能。 所以,现在可能是做出改变的好时机。https://fbflipper.com/docs/stetho.html

它是其他答案加加号的组合

Install DB Browser for SQLite Get the exact location from Device File Explorer for MyBase.db something like "/data/data/com.whatever.myapp/databases/MyBase.db" Set the project window from Android to Project - so you can see the files (build.gradle, gradle.properties,...etc) In Project window right click and chose Open in Terminal 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 Now on the Project window of you Android Studio you have a file "MyBase.db" Double click your db file from Project and you can now browse/edit your databases in DB Browser When you are ready just do Write Changes in DB Browser so databases are saved on hardisk 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.