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

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

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

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


当前回答

这是一个非常古老的问题,我的答案与上面的一些答案相似,但做得更快。下面的脚本是Mac,但我相信有人可以修改它为Windows。

1)在Mac上打开脚本编辑器(你可以在Spotlight中搜索脚本编辑器) 2)复制粘贴下面的文本,并修改你的SDK路径,包名等(见下文) 3)保存脚本!!

这是它!只需按下顶部的播放按钮,即可获得更新后的数据库文件,该文件将在您的桌面上。

替换下面脚本中的内容:

Path_to_my_sdk ==>>把完整的路径到你的SDK My_package_name ==>应用程序的>包名称 myDbName.db ==>数据库的>文件名

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

希望这能帮助到一些人。

其他回答

虽然这是一个很老的问题,但我认为它今天仍然有意义。在最新版本的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 Studio查看数据库:

选项1:

Download and install SQLiteBrowser. 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. 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.


选项2:

转到这个Github存储库,并按照自述文件中的说明在您的设备上查看您的数据库。你得到的结果是这样的:

就是这样。当然,在发布应用之前,你应该撤销所有这些步骤。

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

$ 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

我使用windows 7,我的设备是一个模拟的android设备API 23。我想对于任何真实的设备都是一样的,只要它是根设备,API不超过23

进入工具-> Android -> Android设备监视器。 进入文件资源管理器。在我的例子中,它在data/data//app_webview/databases/file_0/1中

我必须手动在名为"1"的文件末尾添加。db

这可能不是你想要的答案,但我没有更好的方法从手机下载DB。我的建议是确保您使用的是这种迷你ddms。它将超级隐藏,但如果你不点击非常小的伪装框在程序的左下角。(试着在它上面盘旋,否则你可能会错过它。)

同样,下拉菜单显示没有过滤器(右上)。它实际上有很多不同的方法,你可以通过PPID,名称等监视不同的进程/应用程序。我一直用这个来监控手机,但请记住,我不是在做那种需要120%肯定数据库没有做一些不寻常的事情的开发工作。

希望能有所帮助