在设备上调试sqlite数据库

发布于 2024-11-27 17:54:05 字数 423 浏览 0 评论 0原文

我目前正在开发 Android 的 WiFi 应用程序。我在尝试访问设备上的数据库时遇到问题。在模拟器中调试对我来说不起作用,因为模拟器中不支持 WiFi。我尝试使用

adb pull data/data/package-name/databases/database-name

但我收到错误“权限被拒绝”将数据库文件从设备中拉出。 在此答案中 Android:数据库文件存储在哪里?, Commonsware 建议通过在调试模式下运行来拉取数据库文件。但它也不起作用。任何关于如何在不root设备的情况下调试数据库的帮助将不胜感激。

I am presently working on an WiFi application for Android. I am having trouble trying to access the database on the device. Debugging in the emulator doesn't work for me, because there is no WiFi support in the emulator. I tried pulling the database file out of the device by using

adb pull data/data/package-name/databases/database-name

But I get the error "Permission denied.".
In this answer Android: Where are database files stored?, Commonsware has suggested to pull database file by running in debug mode. But it doesn't work too. Any help on how to debug the database without rooting the device would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

世界和平 2024-12-04 17:54:06

我将重复一下另一个答案

从 API 级别 8 (Android 2.2) 开始,如果您将应用程序构建为可调试的,则可以使用 shell run-as 命令作为特定用户/应用程序运行命令或可执行文件,或者只需切换到应用程序的 UID 即可访问其 data 目录。

将文件复制到设备上的外部存储

因此,如果您希望从设备中提取应用程序数据库,您可以从 Android Studio 启动应用程序的调试版本,使用 adb shell 连接并运行以下命令:

run-as com.your.package sh -c "cat ~/databases/db-file.db" > /sdcard/db-file.db

或者您可以使用使用相对路径的较短版本,并通过跳过对主目录的引用 ~ 也可以省略 sh -c 部分:

run-as com.your.package cat databases/db-file.db > /sdcard/db-file.db

这将复制您的 db-file.db 到 SD 卡的根目录 /外部存储。现在,您可以使用文件管理器、adb pull 或任何您喜欢的其他方式轻松地从那里获取它。请注意,使用这种方法,您的应用不需要拥有 WRITE_EXTERNAL_STORAGE 权限,因为复制是由始终可以写入外部存储的 shell 用户完成的。

直接将文件复制到计算机

您还可以使用以下命令将数据库直接复制到计算机:

adb shell 'run-as com.your.package cat databases/db-file.db' > db-file.db

由于 CR/LF<,上面的 adb shell 命令在 Windows 主机上无法正常工作/code> 符号转换,除非您从 bash shell 运行它或使用未记录的 adb exec-out 命令(一些评论提到他们仍然无法让它正常工作)在 Windows 计算机上,因此您的情况可能会有所不同):

adb exec-out 'run-as com.your.package cat databases/db-file.db' > db-file.db

写入文件返回设备

如果您修改了计算机上的文件并希望将修改写回设备,请使用以下命令:

adb exec-in 'run-as com.your.package tee databases/db-file.db' < db-file.db

或者如果您更喜欢 adb shell 版本而不是未记录的 exec-in,使用以下命令(但请阅读上面有关 Windows CR/LF 转换的问题):

adb shell 'run-as com.your.package tee databases/db-file.db' < db-file.db >/dev/null
状态更新

截至 2023 年 7 月,上述所有命令仍然有效

I'll repeat myself from another answer:

Starting from API level 8 (Android 2.2), if you build the application as debuggable, you can use the shell run-as command to run a command or executable as a specific user/application or just switch to the UID of your application so you can access its data directory.

Copy files to external storage on the device

So if you wish to pull your application database from the device you can launch a debug build of the application from Android Studio, connect with adb shell and run the following command:

run-as com.your.package sh -c "cat ~/databases/db-file.db" > /sdcard/db-file.db

or you can use a shorter version which uses relative paths and by skipping the reference to the home directory ~ can omit the sh -c part too:

run-as com.your.package cat databases/db-file.db > /sdcard/db-file.db

This will copy your db-file.db to the root of your SD card / external storage. Now you can easily get it from there by using file manager, adb pull or whatever else you like. Note that with this approach, there is NO need for your app to have WRITE_EXTERNAL_STORAGE permission, as the copying is done by the shell user who can always write to the external storage.

Copy files directly to the computer

You can also copy a database directly to your computer with the following command:

adb shell 'run-as com.your.package cat databases/db-file.db' > db-file.db

The adb shell command above will not work correctly on Windows host because of the CR/LF symbols conversion, unless you run it from a bash shell or use the undocumented adb exec-out command instead (some comments mention they still cannot get it to work correctly on a Windows machine, so your mileage may vary):

adb exec-out 'run-as com.your.package cat databases/db-file.db' > db-file.db

Write files back to the device

If you modified the file on your computer and wish to write the modifications back to the device use the following command:

adb exec-in 'run-as com.your.package tee databases/db-file.db' < db-file.db

or if you prefer an adb shell version over undocumented exec-in, use the following (but read the concern about the Windows CR/LF conversion above):

adb shell 'run-as com.your.package tee databases/db-file.db' < db-file.db >/dev/null
Status update

All the commands above are still working as of July, 2023

小嗲 2024-12-04 17:54:06

我在我的 MAC 上使用这个 shell 脚本,它将数据库直接复制到我的主文件夹。简单的一键解决方案,只需更改包名称 (com.example.app) 和数据库名称 (database.sqlite)

简单脚本脚本

#!/bin/bash
adb -d shell 'run-as com.example.app cat /data/data/com.example.app/databases/database.sqlite > /sdcard/database.sqlite'
adb pull /sdcard/database.sqlite ~/

接受参数 [package_name] [database] 的

#!/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 $1 cat /data/data/$1/databases/$2 > /sdcard/$2' "
cmd2="$ADB_PATH pull /sdcard/$2 $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

I use this shell script on my MAC, that copies database directly to my home folder. Easy one click solution, just change package name (com.example.app) and database name (database.sqlite)

Simple Script

#!/bin/bash
adb -d shell 'run-as com.example.app cat /data/data/com.example.app/databases/database.sqlite > /sdcard/database.sqlite'
adb pull /sdcard/database.sqlite ~/

Script which accepts arguments [package_name] [database]

#!/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 $1 cat /data/data/$1/databases/$2 > /sdcard/$2' "
cmd2="$ADB_PATH pull /sdcard/$2 $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
北斗星光 2024-12-04 17:54:06

查看和管理 Android 应用程序数据库的最佳方法是使用此库 https://github.com/sanathp/DatabaseManager_For_Android

使用此库,您可以从应用程序本身管理应用程序 SQLite 数据库。
您可以查看应用程序数据库中的表,更新、删除、向表中插入行。应用程序中的一切。

它是一个单一的java活动文件,只需将java文件添加到您的源文件夹中。开发完成后,从您的src文件夹中删除java文件即可。

它对我帮助很大。希望它也对你有帮助。

您可以在此处观看 1 分钟演示:http://youtu.be/P5vpaGoBlBY

The best way to view and manage you android app database is to use this library https://github.com/sanathp/DatabaseManager_For_Android

With this library you can manage your app SQLite database from you app itself.
you can view the tables in your app database , update ,delete, insert rows to your tables .Everything from your app.

Its a single java activity file ,just add the java file to your source folder.When the development is done remove the java file from your src folder thats it .

It helped me a lot .Hope it helps you too .

You can view the 1 minute demo here : http://youtu.be/P5vpaGoBlBY

韬韬不绝 2024-12-04 17:54:06

虽然这是一个老问题,但我认为它仍然相关并且值得当前状态的答案。有一些可用的工具允许您直接检查数据库(无需从设备或模拟器中提取它们)。

我最近发现(也是最喜欢)的工具是 Android 调试数据库

您只需要添加此依赖项:

debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'

不需要更多代码。
启动应用程序后,打开 logcat 并过滤“DebugDB”,您会发现一条消息,说明

D/DebugDB: Open http://192.168.178.XXX:8080 in your browser

它适用于每个浏览器,您可以检查数据库表和共享首选项。

输入图像描述这里

它也适用于默认模拟器和 Genymotion 模拟器。


我之前使用的工具是stetho

缺点:你需要添加一些代码并且绑定到 Chrome 浏览器。

优点:您还可以选择检查网络流量。

Although, it's an old question I think it's still relevant and deserves a current state answer. There are tools available, which allow you to inspect databases directly (without the need to pull them from the device or emulator).

The tool, I most recently discovered (and favor most) is Android Debug Database.

You only need to add this dependency:

debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'

No further code is required.
After you started your app, open logcat and filter for "DebugDB" and you will find a message saying

D/DebugDB: Open http://192.168.178.XXX:8080 in your browser

It works with every browser and you can inspect your database tables and shared preferences.

enter image description here

It also works with the default and the Genymotion emulators.


The tool I used before is stetho.

Downside: You need to add a bit of code and you are bound to the Chrome browser.

Advantage: You have the option to also inspect network traffic.

摘星┃星的人 2024-12-04 17:54:06

在新的 Android Studio 4.1 中,有新的 数据库检查员

https://i.ibb.co/9tNM1xg/database-Inspector.gif

您可以选择以下内容菜单栏的选项 View >工具窗口> Database Inspector 打开它(Android Studio 4.2 中的App Inspector)。更详细的说明可以在此博客探索 Android Studio 中的数据库检查器 中等文章。

另一种方法是使用 stetho 来实现此目的。您添加依赖项,然后可以在设备插入时使用 Chrome DevTools ( chrome://inspect ) 检查数据库。

In the new Android Studio 4.1 there is the new Database Inspector.

https://i.ibb.co/9tNM1xg/database-Inspector.gif

You can select the following options from the menu bar View > Tool Windows > Database Inspector to open it (App Inspector in Android Studio 4.2). More detailed instructions can be found in this blog and in Exploring the Database Inspector in Android Studio medium article.

Another way is to use stetho for this. You add the dependency and then can use the Chrome DevTools ( chrome://inspect ) to check the database when the device is plugged in.

听闻余生 2024-12-04 17:54:06

在我的应用程序中,我将数据库导出到 SD 卡。一旦数据库位于 SD 卡上,就可以通过将设备插入计算机来访问它。

看这篇文章:在 Android 上将数据库备份到 SDCard

In my application I export the database to the SD card. Once the database is on the SD card it can be accessed by plugging the device into your computer.

Look at this post: Making a database backup to SDCard on Android

撩发小公举 2024-12-04 17:54:06

如果你尝试

The system cannot find the path specified.

注意

adb -d shell "run-as com.yourpackage cat /data/data/com.yourpackage/databases/dbname.sqlite > /sdcard/dbname.sqlite"

双引号!

If you get

The system cannot find the path specified.

try

adb -d shell "run-as com.yourpackage cat /data/data/com.yourpackage/databases/dbname.sqlite > /sdcard/dbname.sqlite"

Note the double quote!

情何以堪。 2024-12-04 17:54:06

我只是这样做了:

$ adb shell
shell@android:/ $ run-as myapp.package.name sh
shell@android:/data/data/myapp.package.name $

然后我可以调试 sqlite 数据库或使用正确的权限从 shell 执行任何我想做的事情。

I simply did:

$ adb shell
shell@android:/ $ run-as myapp.package.name sh
shell@android:/data/data/myapp.package.name $

Then I can debug an sqlite database or whatever I wanna do from shell with the right permissions.

悲凉≈ 2024-12-04 17:54:06

Android Studio 4.1 添加了查看/编辑 Android SQLite 数据库的新功能。

输入图片description here

如何打开Database Inspector

要在Android Studio 中打开Database Inspector,您需要选择View >工具窗口>菜单栏中的数据库检查器

此外,您还需要在运行 API 级别 26 或更高级别的设备上运行应用程序。

使用此工具,您可以

  • 查询您的数据库

  • 修改和调试您的数据库

在此处输入图像描述

Android Studio 4.1 Added a new feature to view/ edit Android SQLite databases.

enter image description here

How to open Database Inspector

To open the Database Inspector in Android Studio, you need to select View > Tool Windows > Database Inspector from the menu bar.

Also you need to run the app to a device running API level 26 or higher.

Using this tool you can

  • Query your databases

  • Modify and debug your database

enter image description here

晨曦÷微暖 2024-12-04 17:54:06

如果 apk 可调试,则有一种方法可以使用(非 root)adb shell 中名为 run-as 的程序来复制应用程序的私有文件。

There is a way if an apk is debuggable to use a program called run-as from the (non-root) adb shell to copy an application's private file.

筱果果 2024-12-04 17:54:06

这是分步说明 - 大部分取自其他答案的组合。这适用于未解锁的设备。

  1. 连接您的设备并在调试模式下启动应用程序。

  2. 将数据库文件从应用程序文件夹复制到 SD 卡:执行:

    ./adb -d shell 'run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite'

  3. 将数据库文件拉到您的计算机上:执行:

    ./adb pull /sdcard/ 执行:./adb

  4. 安装 Firefox SQLLite 管理器:< a href="https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/" rel="nofollow">https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  5. 打开 Firefox SQLLite Manager 并打开上面第 3 步中的数据库文件。< /p>

  6. 享受吧!

Here is step by step instructions - mostly taken from a combination of the other answers. This works with devices that are not unlocked.

  1. Connect your device and launch the application in debug mode.

  2. Copy the database file from your application folder to your sd card: execute:

    ./adb -d shell 'run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite'

  3. Pull the database files to your machine: execute:

    ./adb pull /sdcard/ execute: ./adb

  4. Install Firefox SQLLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  5. Open Firefox SQLLite Manager and open your database file from step 3 above.

  6. Enjoy!

风蛊 2024-12-04 17:54:06

您需要以 root 身份运行 adb,或者在 root 手机上使用它。

要以 root 身份运行 adb,请使用 adb root

另请参阅:为什么使用 adb 时数据文件夹的访问被拒绝?

You need to be running adb as root, or be using it on a rooted phone.

To run adb as root, use adb root

See also: Why do I get access denied to data folder when using adb?

执手闯天涯 2024-12-04 17:54:06

在 Android 4.4.2 上,run-as-and-cat-to-sdcard 解决方案都不适合我。我不确定,但我怀疑这可能是由于 run-as 工具无法正确处理新的 sdcard_rsdcard_rw 权限。

我首先必须将数据库文件复制到应用程序私有内部存储中的 /files

shell@hammerhead:/ $ run-as com.example.myapp   
shell@hammerhead:/data/data/com.example.myapp $ cp databases/mydb files/mydb

然后复制到 /sdcard/Android/data/com.example.myapp/files在 Javaland 中(这需要 WRITE_EXTERNAL_STORAGE 权限):

public class MainActivity extends BaseActivity {

    @Override
     protected void onCreate(Bundle savedInstanceState) {
         ...

         if (isExternalStorageWritable()) {
             final FileInputStream input;
             try {
                 input = openFileInput("mydb");

                 File output = new File(getExternalFilesDir(null), "mydb");

                 copy(input, output);
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }

     public void copy(FileInputStream in, File dst) throws IOException {
         OutputStream out = new FileOutputStream(dst);

         // Transfer bytes from in to out
         byte[] buf = new byte[1024];
         int len;
         while ((len = in.read(buf)) > 0) {
             out.write(buf, 0, len);
         }
         in.close();
         out.close();
     }

     public boolean isExternalStorageWritable() {
         String state = Environment.getExternalStorageState();
         return Environment.MEDIA_MOUNTED.equals(state);
     }
 }

最后,我将文件复制到我的笔记本电脑上:

$ adb pull /sdcard/Android/data/com.example.myapp/files/mydb

None of the run-as-and-cat-to-sdcard solutions worked for me on Android 4.4.2. I'm not sure, but I suspect it may be due to the run-as tool not correctly handling the new sdcard_r and sdcard_rw permissions.

I first had to copy the database file to /files in my application's private internal storage:

shell@hammerhead:/ $ run-as com.example.myapp   
shell@hammerhead:/data/data/com.example.myapp $ cp databases/mydb files/mydb

Then I copied to /sdcard/Android/data/com.example.myapp/files in Javaland (this requires the WRITE_EXTERNAL_STORAGE permission):

public class MainActivity extends BaseActivity {

    @Override
     protected void onCreate(Bundle savedInstanceState) {
         ...

         if (isExternalStorageWritable()) {
             final FileInputStream input;
             try {
                 input = openFileInput("mydb");

                 File output = new File(getExternalFilesDir(null), "mydb");

                 copy(input, output);
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }

     public void copy(FileInputStream in, File dst) throws IOException {
         OutputStream out = new FileOutputStream(dst);

         // Transfer bytes from in to out
         byte[] buf = new byte[1024];
         int len;
         while ((len = in.read(buf)) > 0) {
             out.write(buf, 0, len);
         }
         in.close();
         out.close();
     }

     public boolean isExternalStorageWritable() {
         String state = Environment.getExternalStorageState();
         return Environment.MEDIA_MOUNTED.equals(state);
     }
 }

Finally, I copied the file to my laptop:

$ adb pull /sdcard/Android/data/com.example.myapp/files/mydb
离鸿 2024-12-04 17:54:06

我的设备没有 SD 卡

so the first solution did not work for me.

如果您遇到类似问题,请尝试这样:

  adb shell "run-as package chmod 777 /data/data/package/databases/yourdb.sqlite";
  adb pull /data/data/package/databases/yourdb.sqlite

My Device was not having sdcard

so the first solution did not work for me.

If you are having similar issue try like this:

  adb shell "run-as package chmod 777 /data/data/package/databases/yourdb.sqlite";
  adb pull /data/data/package/databases/yourdb.sqlite
你的往事 2024-12-04 17:54:06

试试这个应用程序:SQLiteWeb (https://play.google .com/store/apps/details?id=br.com.cm.sqliteweb)。它提供对数据库的远程访问,而无需将其拉出。

在付费版本中,它具有私有数据库的根访问权限(/data/data/package-name...)或实现内容提供程序以使用 SQLiteWeb 连接(应用程序内的说明)

但如果想保留免费版本,您可以在外部存储中创建数据库:

database = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()
        + "/" + DATABASE_NAME, null, DATABASE_VERSION);

Try this app: SQLiteWeb (https://play.google.com/store/apps/details?id=br.com.cm.sqliteweb). It provides remote access to your database without pulling it out.

In paid version, it has root access for private database (/data/data/package-name...) or implement a Content Provider to connect using SQLiteWeb (Instructions inside app)

But if want to stay with the free version, you may create your database in external storage:

database = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()
        + "/" + DATABASE_NAME, null, DATABASE_VERSION);
空‖城人不在 2024-12-04 17:54:06

在 OSX 上,使用 @Tadas 答案以及 automator 和 sqllitebrowser(https://github.com/sqlitebrowser/sqlitebrowser ):

  1. 打开 Automator 并创建新工作流程。

  2. 添加“运行 Shell 脚本”操作。

  3. 粘贴此:

    源~/.bash_profile
    adb shell 'run-as cat /data/data/your_app_uid/databases/db.name > /tmp/db.name'
    adb pull /tmp/db.name ~/
    open -a sqlitebrowser ~/db.name

  4. 单击运行以刷新 sqlitebrowser 上的数据库。

On OSX,using @Tadas answer with automator and sqllitebrowser(https://github.com/sqlitebrowser/sqlitebrowser):

  1. open Automator and create new workflow.

  2. add "Run Shell Script" action.

  3. Paste this :

    source ~/.bash_profile
    adb shell 'run-as cat /data/data/your_app_uid/databases/db.name > /tmp/db.name'
    adb pull /tmp/db.name ~/
    open -a sqlitebrowser ~/db.name

  4. click run to refresh the database on sqlitebrowser.

岁吢 2024-12-04 17:54:06
  1. 打开终端
  2. cd (对于 Windows 上的我来说 cd C:\Users\Willi\AppData\Local\Android\sdk
  3. cd platform- tools
  4. adb shell (仅当只有一个模拟器运行时才有效)
  5. cd data/data
  6. su (获得超级用户权限)
  7. <代码>光盘/databases
  8. sqlite3
  9. 发出 SQL 语句(重要:; 终止它们,否则语句不会发出,而是换行。)

注意:如果需要列出目录内容,请使用 ls (Linux) 或 dir (Windows)。

  1. Open up a terminal
  2. cd <ANDROID_SDK_PATH> (for me on Windows cd C:\Users\Willi\AppData\Local\Android\sdk)
  3. cd platform-tools
  4. adb shell (this works only if only one emulator is running)
  5. cd data/data
  6. su (gain super user privileges)
  7. cd <PACKAGE_NAME>/databases
  8. sqlite3 <DB_NAME>
  9. issue SQL statements (important: terminate them with ;, otherwise the statement is not issued and it breaks to a new line instead.)

Note: Use ls (Linux) or dir (Windows) if you need to list directory contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文