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

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

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

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


当前回答

Android Studio从4.1版开始就绑定了一个数据库检查器。

但它有一个局限性:

数据库检查器仅适用于API级别26及更高的Android操作系统中包含的SQLite库。它不能与你的应用程序捆绑的其他SQLite库一起使用。

其他回答

Follow the above steps to open the database folder of your app in Android Device explorer and in there you can see six files, The first three is named as android and the last three is named as your database name. You only have to work with the last three files, save these three files at your preferred location. You can save these files by right-clicking to the file and click save as button (like I have my database named as room_database and so three files are named as room_database, room_database-shm, and room_database-wal. Rename the corresponding file as follows:-

1)从room_database到room_database.db 2) room_database-shm到room_database.db-shm 3) room_database-wal到room_database.db-wal

现在在任何SQLite浏览器中打开第一个文件,这三个文件都将被填充到浏览器中。

在Android Studio 3及以上,你可以在Android Studio右下角看到一个“设备文件资源管理器”部分。

打开它,然后你可以看到文件树,你可以在这个路径下找到一个应用程序数据库:

/data/data/{package_name}/databases/

右键单击db文件并另存为…(然后保存到桌面)。

然后使用SQLite sqlitebrowser.org(先下载并安装),单击“打开数据库”,选择保存的*.db文件,然后选择“浏览数据”选项卡并选择表以查看其内容。

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

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

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.

通过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