Android 模拟器在哪里存储 SQLite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1510840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:10:10  来源:igfitidea点击:

Where does Android emulator store SQLite database?

androidsqlite

提问by I82Much

I'm working on an Android application that stores data in a SQLite database. My question is, where does this database file get stored on the filesystem when you're using an emulator?

我正在开发一个将数据存储在 SQLite 数据库中的 Android 应用程序。我的问题是,当您使用模拟器时,此数据库文件存储在文件系统的何处?

I have seen that it's stored in

我已经看到它存储在

/data/data/package_name/databases

/data/data/package_name/databases

but I need to know where on my local machine's hard drive that actually maps to. The database persists on multiple runs of the emulator, even after having shut down the machine, so it can't just reside in RAM...

但我需要知道本地机器硬盘上实际映射到的位置。数据库在模拟器的多次运行中仍然存在,即使在关闭机器之后,它也不能仅仅驻留在 RAM 中......

采纳答案by Eric Mill

The filesystem of the emulator doesn't map to a directory on your hard drive. The emulator's disk image is stored as an image file, which you can manage through either Eclipse (look for the G1-looking icon in the toolbar), or through the emulator binary itself (run "emulator -help" for a description of options).

模拟器的文件系统不会映射到硬盘驱动器上的目录。模拟器的磁盘映像存储为映像文件,您可以通过 Eclipse(在工具栏中查找看起来像 G1 的图标)或通过模拟器二进制文件本身(运行“emulator -help”以获得选项描述)来管理该文件.

You're best off using adb from the command line to Hyman into a running emulator. If you can get the specific directory and filename, you can do an "adb pull" to get the database file off of the emulator and onto your regular hard drive.

您最好从命令行使用 adb 来进入正在运行的模拟器。如果您可以获得特定的目录和文件名,您可以执行“adb pull”以将数据库文件从模拟器中取出并放到您的常规硬盘上。

Edit: Removed suggestion that this works for unrooted devices too - it only works for emulators, and devices where you are operating adb as root.

编辑:删除了这也适用于无根设备的建议 - 它仅适用于模拟器和您以 root 身份操作 adb 的设备。

回答by rogcg

An update mentioned in the comments below:

以下评论中提到的更新:

You don't need to be on the DDMS perspective anymore, just open the File Explorer from Eclipse Window > Show View > Other...It seems the app doesn't need to be running even, I can browse around in different apps file contents. I'm running ADB version 1.0.29

您不再需要处于 DDMS 视角,只需从 Eclipse窗口 > 显示视图 > 其他打开文件资源管理器...似乎该应用程序甚至不需要运行,我可以浏览不同的应用程序文件内容。我正在运行亚行版本 1.0.29



Or, you can try the old approach:

或者,您可以尝试旧方法:

Open the DDMS perspective on your Eclipse IDE

在 Eclipse IDE 上打开 DDMS 透视图

(Window > Open Perspective > Other > DDMS)

(窗口 > 打开透视图 > 其他 > DDMS)

and the most important:

以及最重要的:

YOUR APPLICATION MUST BE RUNNING SO YOU CAN SEE THE HIERARCHY OF FOLDERS AND FILES.

您的应用程序必须正在运行,以便您可以看到文件夹和文件的层次结构

Then in the File Explorer Tabyou will follow the path :

然后在文件资源管理器选项卡中,您将遵循路径:

data > data > your-package-name > databases > your-database-file.

数据 > 数据 > 你的包名 > 数据库 > 你的数据库文件

Then select the file, click on the disket iconin the right corner of the screen to download the .dbfile. If you want to upload a database file to the emulator you can click on the phone icon(beside disket icon)and choose the file to upload.

然后选择文件,点击屏幕右上角的磁盘图标下载.db文件。如果要将数据库文件上传到模拟器,您可以单击电话图标(磁盘图标旁边)并选择要上传的文件。

If you want to see the content of the .dbfile, I advise you to use SQLite Database Browser, which you can download here.

如果你想查看.db文件的内容,我建议你使用 SQLite 数据库浏览器,你可以在这里下载。

PS: If you want to see the database from a real device, you must root your phone.

PS:如果你想在真机上看到数据库,你必须root你的手机。

回答by Yuchen Zhong

The other answers are severely outdated. With Android Studio, this is the way to do it:

其他答案已经严重过时了。使用Android Studio,这是这样做的方法:

  1. Click on Tools > Android > Android Device Monitor
  1. 单击工具 > Android > Android 设备监视器

enter image description here

在此处输入图片说明

  1. Click on File Explorer
  1. 单击文件资源管理器

enter image description here

在此处输入图片说明

  1. Navigate to your db file and click on the Savebutton.
  1. 导航到您的 db 文件,然后单击“保存”按钮。

enter image description here

在此处输入图片说明

回答by S. Miller

In Android Studio 3.4.1, you can use the search feature of Android Studio to find "Device File Explorer" and then go to the /data/data/package_name/database directory of your emulator.

在 Android Studio 3.4.1 中,您可以使用 Android Studio 的搜索功能找到“设备文件资源管理器”,然后转到模拟器的 /data/data/package_name/database 目录。

回答by Foyzul Karim

Since the question is not restricted to Android Studio, So I am giving the path for Visual Studio 2015 (worked for Xamarin).

由于问题不限于 Android Studio,所以我给出了 Visual Studio 2015(适用于 Xamarin)的路径。

Tools-Android-Android Device Monitor

工具-Android-Android 设备监控

enter image description here

在此处输入图片说明

  • Locate the database file mentioned in above image, and click on Pull button as it shown in image 2.
  • Save the file in your desired location.
  • You can open that file using SQLite Studio or DB Browser for SQLite.
  • 找到上图中提到的数据库文件,然后单击“拉取”按钮,如图 2 所示。
  • 将文件保存在所需位置。
  • 您可以使用 SQLite Studio 或 DB Browser for SQLite 打开该文件。

Special Thanks to other answerers of this question.

特别感谢这个问题的其他回答者。

回答by Tadas Valaitis

I wrote a simple bash script, which pulls database from android device to your computer (Linux, Mac users)

我写了一个简单的 bash 脚本,它将数据库从 android 设备拉到您的计算机(Linux、Mac 用户)

filename:android_db_move.sh usage: android_db_move.sh com.example.app db_name.db

文件名:android_db_move.sh 用法:android_db_move.sh com.example.app db_name.db

#!/bin/bash

REQUIRED_ARGS=2
ADB_PATH=/Users/Tadas/Library/sdk/platform-tools/adb
PULL_DIR="~/"

if [ $# -ne $REQUIRED_ARGS ]
    then
        echo ""
        echo "Usage:"
        echo "android_db_move.sh [package_name] [db_name]"
        echo "eg. android_db_move.sh lt.appcamp.impuls impuls.db"
        echo ""
    exit 1
fi;


echo""

cmd1="$ADB_PATH -d shell 'run-as  cat /data/data//databases/ > /sdcard/' "
cmd2="$ADB_PATH pull /sdcard/ $PULL_DIR"

echo $cmd1
eval $cmd1
if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

echo $cmd2
eval $cmd2

if [ $? -eq 0 ]
    then
    echo ".........OK"
fi;

exit 0

回答by caligari

The databases are stored as SQLite files in /data/data/PACKAGE/databases/DATABASEFILEwhere:

数据库作为 SQLite 文件存储在/data/data/PACKAGE/databases/DATABASEFILE 中,其中:

You can see (copy from/to filesystem) the database file in the emulator selecting DDMS perspective, in the File Explorer tab.

您可以在选择DDMS 透视图的模拟器中查看(从文件系统复制)数据库文件,在文件资源管理器选项卡中。

回答by Mick

Simple Solution - works for both Emulator & Connected Devices

简单的解决方案 - 适用于模拟器和连接设备

1See the list of devices/emulators currently available.

1查看当前可用的设备/模拟器列表。

$ adb devices

List of devices attached

G7NZCJ015313309 device emulator-5554 device

9885b6454e46383744 device

连接的设备列表

G7NZCJ015313309 设备仿真器-5554 设备

9885b6454e46383744 设备

2Run backup on your device/emulator

2在您的设备/模拟器上运行备份

$ adb -s emulator-5554 backup -f ~/Desktop/data.ab -noapk com.your_app_package.app;

3Extract data.ab

3提取数据.ab

$ dd if=data.ab bs=1 skip=24 | openssl zlib -d | tar -xvf -;

You will find the database in /dbfolder

您将在/db文件夹中找到数据库

回答by vahid sabet

according to Android docs, Monitor was deprecated in Android Studio 3.1 and removed from Android Studio 3.2. To access files, there is a tab in android studio called "Device File Explorer" bottom-right side of developing window which you can access your emulator file system. Just follow

根据Android 文档,Monitor 在 Android Studio 3.1 中已弃用,并从 Android Studio 3.2 中删除。要访问文件,android studio 中有一个名为“设备文件资源管理器”的选项卡,在开发窗口的右下角,您可以访问您的模拟器文件系统。只需关注

/data/data/package_name/databases

/data/data/package_name/databases

good luck.

祝你好运。

Android device File explorer

Android 设备文件浏览器

回答by Ana M

For Android Studio 3.5, fount it using instructions here: https://developer.android.com/studio/debug/device-file-explorer(View -> Tool Windows -> Device File Explorer -> -> databases

对于 Android Studio 3.5,请使用此处的说明获取它:https: //developer.android.com/studio/debug/device-file-explorer(查看 -> 工具窗口 -> 设备文件资源管理器 -> -> 数据库