自应用发布以来,我一直在使用Android Studio开发应用。
直到最近,一切都很好,我必须调试和检查数据库文件。由于我不知道如何直接看到数据库,当我调试生成数据库文件时,我不得不将数据库文件从我的手机导出到PC上。
为此,我必须打开DDMS >文件资源管理器。一旦我打开DDMS,我必须重新连接USB,我失去了调试线程。在检查数据库文件之后,我必须关闭DDMS并重新连接USB以回到调试模式。
这太复杂了。有人有更好的方法来做到这一点在Android Studio(我知道它更容易在Eclipse) ?
从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存储库,并按照自述文件中的说明在您的设备上查看您的数据库。你得到的结果是这样的:
就是这样。当然,在发布应用之前,你应该撤销所有这些步骤。
最后,我找到了一个最简单的解决方案,不需要打开DDMS。
实际上,解决方案是基于@Distwo提到的,但它不必那么复杂。
首先,记住数据库文件在设备中的路径,它应该始终相同。
例如我的是:/data/data/com.XXX.module/databases/com.XXX.module.database
其次,执行这个命令,将数据库文件拉到PC上
adb pull /data/data/com.XXX.module/databases/com.XXX.module.database /Users/somePathOnYourPC/
您需要做的就是复制并存储这个命令,然后您就可以一次又一次地使用它。
第三,如果你得到了Permission Denied或类似的情况,只需在前面的命令之前运行adb root。
通过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
要知道sqlite数据库存储由你在android studio,你需要遵循简单的步骤:
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.
欲了解更多信息,此链接将是有用的。
http://www.c-sharpcorner.com/UploadFile/e14021/know-where-database-is-stored-in-android-studio/
要查看您的db文件中的数据,您需要下载sqlite浏览器或在任何浏览器中添加相同的插件,因此您需要在浏览器中打开文件并查看您的数据。
从http://sqlitebrowser.org/下载浏览器
这里是屏幕截图显示浏览器包含注册数据库
谢谢,