如何查看Android设备上的SQLite数据库?

我在SQLite数据库中有一组数据。 我需要查看设备上的数据库。 我怎么做?

我检查了ddms模式。 文件资源pipe理器中的数据是空的。

这里是一步一步的说明(主要是从其他答案的组合)。 即使在没有固定的设备上也能正常工作。

  1. 连接您的设备并以debugging模式启动应用程序。

  2. 您可能需要使用adb -d shell "run-as com.yourpackge.name ls /data/data/com.yourpackge.name/databases/"来查看数据库文件名是什么。

注意: com.yourpackge.name是您的应用程序包名称。 你可以从清单文件中得到它。

  1. 将数据库文件从应用程序文件夹复制到SD卡。

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

注意: filename.sqlite是您在创build数据库时使用的数据库名称

  1. 将数据库文件拖到您的机器上:

    adb pull /sdcard/filename.sqlite

这将从SD卡将数据库复制到您的ADB存在的地方。

  1. 安装Firefox SQLitepipe理器: https : //addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  2. 打开Firefox的SQLitepipe理器(工具 – > SQLitepipe理器),并从上面的步骤3打开您的数据库文件。

  3. 请享用!

查看和pipe理您的Android应用程序数据库的最好方法是使用库DatabaseManager_For_Android 。

这是一个Java活动文件; 只需将其添加到您的源文件夹。 您可以查看您的应用程序数据库中的表,更新,删除,插入行到您的表。 一切从你的应用程序。

开发完成后,从src文件夹中删除Java文件。 而已。

您可以查看5分钟演示, 数据库pipe理器的Android SQLite数据库

从这里采取,你可以尝试:

  1. Facebook的开源Stetho库

在build.gradle:

 dependencies { // Stetho core compile 'com.facebook.stetho:stetho:1.5.0' //If you want to add a network helper compile 'com.facebook.stetho:stetho-okhttp:1.5.0' } 

初始化应用程序对象中的库:

 Stetho.initializeWithDefaults(this); 

你可以通过chrome://inspect在Chrome中查看你的数据库

  1. 另一个select是这个插件 (不是免费的)
  2. 最后一个是这个免费/开源的库,在浏览器中查看数据库内容https://github.com/amitshekhariitbhu/Android-Debug-Database

到目前为止,我发现的最好的方法是使用Androiddebugging数据库工具。

它的使用和设置令人难以置信的简单,只需添加依赖和通过networking连接到设备数据库的接口。 无需根植电话或添加任何活动。 这里是步骤:

步骤1

将以下依赖添加到您的应用程序的gradle文件并运行该应用程序。

 debugCompile 'com.amitshekhar.android:debug-db:1.0.0' 

第2步

打开你的浏览器,在8080端口上访问你的手机的IP地址http://YOUR_PHONE_IP_ADDRESS:8080应该是这样的: http://YOUR_PHONE_IP_ADDRESS:8080 。 您将看到以下内容:

在这里输入图像说明

为了获得myphone的IP我目前使用Ping工具,但有很多的select。

第3步

而已!


在官方文档中有更多的细节: https : //github.com/amitshekhariitbhu/Android-Debug-Database

如果您使用的是真实设备,并且没有根目录,则无法在FileExplorer查看您的数据库,因为出于某种安全原因,该文件夹被locking在Android系统中。 如果您在模拟器中使用它,您可以在FileExplorer/ data / data /您的软件包名称/ databases / yourdatabse.db中find它。

你可以这样做:

  1. adb shell
  2. cd /go/to/databases
  3. sqlite3 database.db
  4. sqlite>提示符下,input.tables 。 这将为您提供database.db文件中的所有表。
  5. select * from table1;

尝试AndroidDBvieweR

  • 无需您的设备被根植
  • 不需要导入应用程序的数据库文件
  • 几个configuration,你很好走!

AndroidDBvieweR

如果你不想使用

  1. 亚行
  2. 第三方库
  3. gradle依赖

只需要SD卡中的数据库文件,这是解决scheme。

在你的项目中复制类DatabaseUtil.java

 package com.util; import android.content.Context; import android.os.Environment; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; /** * Shayan Rais (http://shanraisshan.com) * created on 8/17/2016 */ public class DatabaseUtil { //You need to declare permission // <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> //in your Manifest file in order to use this class //______________________________________________________________________________________________ //todo -> rename the database according to your application final static String DATABASE_NAME = "MyDatabase.sqlite"; //example WhatsApp : /data/data/com.whatsapp/databases/msgstore.db final static String FOLDER_EXTERNAL_DIRECTORY = Environment.getExternalStorageDirectory() + "/shanraisshan"; //______________________________________________________________________________________________ public static void copyDatabaseToExtStg(Context ctx) { //external storage file File externalDirectory = new File(FOLDER_EXTERNAL_DIRECTORY); if(!externalDirectory.exists()) . . 

只需从应用中的任何活动调用copyDatabaseToExtStg()方法即可


这将复制位于sdcard / shanraisshan / your_database_file的数据库文件夹

该图显示了SD卡中的MyDatabase.sqlite


有关更详细的说明,请查看 Github上的Android-Database-Viewer 。 https://github.com/shanraisshan/Android-Database-Viewer

虽然这不会直接在设备上查看数据库,但我已经发布了一个简单的shell脚本,用于将数据库转储到本地计算机:

https://github.com/Pixplicity/dbdump

它执行这里描述的两种不同的方法:

  1. 首先,它试图使其他用户可以访问该文件,并试图从设备中获取该文件。
  2. 如果失败,则通过terminal将文件的内容stream式传输到本地机器。 它执行一个额外的技巧来删除某些设备输出到shell的\r字符。

从这里您可以使用各种CLI或GUI SQLite应用程序(如sqlite3sqlitebrowser )来浏览数据库的内容。

我一直在使用SQLite数据库浏览器来查看Android开发中的内容SQLite数据库。 您必须首先从设备中提取数据库文件,然后在SQLite数据库浏览器中打开它。

尝试Facebook Stetho。

Stetho是Android应用程序的debugging桥梁,支持强大的Chrome开发者工具等等。

https://github.com/facebook/stetho

希望这可以帮助你

使用terminal首先指出你的位置,在哪里Andriod sdk是loacted

 eg: C:\Users\AppData\Local\Android\sdk\platform-tools> 

然后检查附加使用的设备列表

  adb devices 

然后运行此命令将文件从设备复制到您的系统

 adb -s YOUR_DEVICE_ID shell run-as YOUR_PACKAGE_NAME chmod -R 777 /data/data/YOUR_PACKAGE_NAME/databases && adb -s YOUR_DEVICE_ID shell "mkdir -p /sdcard/tempDB" && adb -s YOUR_DEVICE_ID shell "cp -r /data/data/YOUR_PACKAGE_NAME/databases/ /sdcard/tempDB/." && adb -s YOUR_DEVICE_ID pull sdcard/tempDB/ && adb -s YOUR_DEVICE_ID shell "rm -r /sdcard/tempDB/*" 

您可以在此path中find数据库文件

  Android\sdk\platform-tools\tempDB\databases 

第1步 复制这个类在你的包

第2步将下面的代码放入扩展SQLiteOpenHelper的类中。

  //-----------------for show databasae table---------------------------------------- public ArrayList<Cursor> getData(String Query) { //get writable database SQLiteDatabase sqlDB =this.getWritableDatabase(); String[] columns = new String[] { "mesage" }; //an array list of cursor to save two cursors one has results from the query //other cursor stores error message if any errors are triggered ArrayList<Cursor> alc = new ArrayList<Cursor>(2); MatrixCursor Cursor2= new MatrixCursor(columns); alc.add(null); alc.add (null); try{ String maxQuery = Query ; //execute the query results will be save in Cursor c Cursor c = sqlDB.rawQuery(maxQuery, null); //add value to cursor2 Cursor2.addRow(new Object[] { "Success" }); alc.set(1,Cursor2); if (null != c && c.getCount() > 0) { alc.set(0,c); c.moveToFirst(); return alc ; } return alc; } catch(SQLException sqlEx) { Log.d("printing exception", sqlEx.getMessage()); //if any exceptions are triggered save the error message to cursor an return the arraylist Cursor2.addRow(new Object[] { ""+sqlEx.getMessage() }); alc.set(1,Cursor2); return alc; } catch(Exception ex) { Log.d("printing exception",ex.getMessage()); //if any exceptions are triggered save the error message to cursor an return the arraylist Cursor2.addRow(new Object[] { ""+ex.getMessage() }); alc.set(1,Cursor2); return alc; } } 

步骤3在清单中注册

 <activity android:name=".database.AndroidDatabaseManager" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"/> 

步骤4

 Intent i = new Intent(this, AndroidDatabaseManager.class); startActivity(i); 

按着这些次序

1 >从这里下载* .jar文件。

2 >将* .jar文件放入eclipse / dropins /文件夹中,然后重新启动eclipse。

3 >在eclipse的右上angular,点击DDMS图标。

4 >在左侧面板中select适当的模拟器。

5在主面板的“文件资源pipe理器”选项卡上,转至/data/data/[YOUR.APP.NAMESPACE]/databases。

6 >在DDMS图标下,select数据库时,应该有一个新的数据库蓝色图标点亮。 点击它,你会看到一个Questoid的Sqlitepipe理器标签打开查看您的数据。

*注意:如果数据库不亮,可能是因为您的数据库没有* .db文件扩展名。 确保您的数据库名为[DATABASE_NAME] .db

*注意:如果你想使用没有.db-Extension的数据库:

– 下载此Questoid SqLiteBrowser: 在这里下载 。

解压并放入eclipse / dropins(不是插件)。

– 了解更多信息点击此处 。

这适用于Android 6.0(至less可debugging的应用程序):

 adb shell "run-as your.package.name cp /data/data/your.package.name/databases/you-db-name /sdcard/file_to_write" 

然后你就可以简单地用SQLiteManager查看数据库。

你可以试试SQLiteOnWeb 。 它在浏览器中pipe理你的SQLite数据库。

有TKlerx的EclipseAndroid SQLite浏览器 ,它与Android Studio完全兼容。 我会推荐它,因为它非常实用。

要将其安装在设备监视器上,只需将JAR文件放在[Path to Android SDK folder]/sdk/tools/lib/monitor-[...]/plugins

使用文件资源pipe理器,你可以像这样find你的数据库文件:

 data-->data-->your.package.name-->databases--->yourdbfile.db 

然后你可以使用任何SQLite前端来探索你的数据库。 我使用SQLitepipe理器Firefox插件。 这很好,很小,而且很快。

我发现非常简单的库stetho来浏览铬的应用程序的sqlite数据库, 请参阅

第一篇文章( https://stackoverflow.com/a/21151598/4244605 )不适合我。

我写了自己的脚本来获取设备的DB文件。 没有根。 工作好。

  1. 复制脚本到adb目录(例如: ~/android-sdk/platform-tools )。
  2. 设备必须连接到PC。
  3. 使用./getDB.sh -p <packageName>获取数据库的名称。

用法: ./getDB.sh -p <packageName> -n <name of DB> -s <store in mobile device>用于将DB文件(此处执行脚本)的目录。

我build议你设置DB的文件名为* .sqlite,并用Firefox插件打开它:SQLitepipe理器。

(这是很长的时间,当我用Bash写了一些东西的时候,你可以编辑这个代码。)

 #!/bin/sh # Get DB from Android device. # Hoption=false Poption=false Noption=false Soption=false Parg="" Narg="" Sarg="" #-----------------------FUNCTION--------------------------: helpFunc(){ #help echo "Get names of DB's files in your Android app. Usage: ./getDB -h ./getDB -p packageName -n nameOfDB -s storagePath Options: -h Show help. -p packageName List of databases for package name. -p packageName -n nameOfDB -s storagePath Save DB from device to this directory." } #--------------------------MAIN--------------------------: while getopts 'p:n:s:h' options; do case $options in p) Poption=true Parg=$OPTARG;; n) Noption=true Narg=$OPTARG;; s) Soption=true Sarg=$OPTARG;; h) Hoption=true;; esac done #echo "------------------------------------------------------- #Hoption: $Hoption #Poption: $Poption #Noption: $Noption #Soption: $Soption #Parg: $Parg #Narg: $Narg #Sarg: $Sarg #-------------------------------------------------------"\\n #echo $# #count of params if [ $Hoption = true ];then helpFunc elif [ $# -eq 2 -a $Poption = true ];then #list ./adb -d shell run-as $Parg ls /data/data/$Parg/databases/ exit 0 elif [ $# -eq 6 -a $Poption = true -a $Noption = true -a $Soption = true ];then #get DB file #Change permissions ./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/ ./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/$Narg #Copy ./adb shell cp /data/data/$Parg/databases/$Narg $Sarg #Pull file to this machine ./adb pull $Sarg/$Narg exit 0 else echo "Wrong params or arguments. Use -h for help." exit 1; fi exit 0;