如何在没有root的Android设备上检查数据库

Cod*_*Rat 18 sqlite android adb android-contentprovider android-sqlite

我正在开发一个应用程序,我使用sqllite3数据库来存储值.我有Nexus S和Nexus 7都是无根设备.如何为我的应用程序获取数据库以进行调试.

我试过(1)我尝试过这里提到的所有方法

adb shell
run-as app.package.name \
cp /data/data/package.name/databases/application.sqlite /sdcard/
exit
adb pull /sdcard/application.sqlite ~/
Run Code Online (Sandbox Code Playgroud)

这说cp没找到..

(2)http://developer.android.com/tools/help/adb.html#sqlite

adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit 
Run Code Online (Sandbox Code Playgroud)

kma*_*mur 36

以下解决方案仅适用于可调试的应用程序.它可能无法在所有设备上正常工作,因为run-as命令在某些设备上不起作用,尤其是对于Jelly Bean.

  1. 创建*.bat文件并复制以下脚本

    adb shell run-as [package] chmod 777/data/data/[package]/databases /

    adb shell run-as [package] chmod 777/data/data/[package]/databases/[db_file_name]

    adb shell run-as [package] cp/data/data/[package]/databases/[db_file_name]/sdcard /

    adb pull/sdcard/[db_file_name]

  2. 将[package]更改为所需的应用程序包

  3. 将[db_file_name]更改为所需的数据库名称运行bat文件,您应该在与bat文件相同的文件夹中看到复制的数据库

以上解决方案假定:

  • 您正在使用Windows
  • 设备已在"adb设备"下连接并可见


Tro*_*omB 30

您可以使用以下内容将数据库写入外部存储器:

private void writeToSD() throws IOException {
    File sd = Environment.getExternalStorageDirectory();

    if (sd.canWrite()) {
        String currentDBPath = DB_NAME;
        String backupDBPath = "backupname.db";
        File currentDB = new File(DB_PATH, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

DB_NAME我的数据库的名称在哪里,DB_PATH定义如下:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
    }
    else {
        DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";
    }
Run Code Online (Sandbox Code Playgroud)

并添加以下权限(感谢@Sathesh指出这一点):

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

我随时编写数据库时调用此方法,以便我最新的数据库文件位于外部存储器中,我可以从那里查看和调试.

然后,您可以使用X-Plore应用程序从Android设备上的外部存储器查看数据库.

  • 您需要添加外部存储权限<uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE"/>.您可以使用"context.getDatabasePath(dbName).toString()"以编程方式获取dbpath (2认同)

mr.*_*fox 9

如果您不知道您的应用程序路径,那么您可以使用:

public void copyAppDbToExternalStorage() throws IOException {
    File sd = Environment.getExternalStorageDirectory();
    File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
    if (sd.canWrite()) {
        File backupDB = new File(sd, "toDatabaseName"); // for example "my_data_backup.db"
        if (currentDB.exists()) {
            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者如果您需要将数据库复制到公共"下载"文件夹,那么您可以使用:

public void copyAppDbToDownloadFolder() throws IOException {
    File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "toDatabaseName"); // for example "my_data_backup.db"
    File currentDB = getApplicationContext().getDatabasePath("databaseName"); //databaseName=your current application database name, for example "my_data.db"
    if (currentDB.exists()) {
        FileChannel src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        dst.transferFrom(src, 0, src.size());
        src.close();
        dst.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

这在我的Nexus 4设备上完美运行.


kol*_*nar 9

这是一个非常简单明了的答案:(在Android上测试过:无根)

adb -d shell 
$ run-as my.package.name
$ cp databases/mydatabase.db /sdcard/mydatabase.db
$ exit
$ exit
Run Code Online (Sandbox Code Playgroud)

现在将数据库拉到默认的adb路径

adb -d pull /sdcard/mydatabase.db
Run Code Online (Sandbox Code Playgroud)

或者,例如,到您的桌面

adb -d pull /sdcard/mydatabase.db C:\Users\user\Desktop
Run Code Online (Sandbox Code Playgroud)

您可能想要使用以下命令删除副本:

adb -d shell "rm /sdcard/mydatabase.db"
Run Code Online (Sandbox Code Playgroud)

-d 如果有多个模拟器,则选项选择默认设备.