首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在未启动的android设备上检查数据库

如何在未启动的android设备上检查数据库
EN

Stack Overflow用户
提问于 2013-07-26 22:03:30
回答 8查看 31.6K关注 0票数 19

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

我已经尝试了(1)我已经尝试了here提到的所有方法

代码语言:javascript
复制
adb shell
run-as app.package.name \
cp /data/data/package.name/databases/application.sqlite /sdcard/
exit
adb pull /sdcard/application.sqlite ~/

这显示cp未找到..

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

代码语言:javascript
复制
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 
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2013-07-26 22:08:01

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

代码语言:javascript
复制
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();
        }
    }
}

其中DB_NAME是我的数据库的名称,DB_PATH的定义如下:

代码语言:javascript
复制
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/";
    }

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

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

每当我有一个数据库写操作时,我都会调用这个方法,这样我最新的数据库文件就在外部存储器中,我可以从那里查看和调试它。

然后你就可以使用X-Plore app从安卓设备的外部存储器查看数据库了。

票数 30
EN

Stack Overflow用户

发布于 2013-12-27 12:37:13

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

代码语言:javascript
复制
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();
        }
    }
}

或者,如果您需要将数据库复制到公共“下载”文件夹,则可以使用以下命令:

代码语言:javascript
复制
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();
    }
}

这在我的Nexus4设备上工作得很好。

票数 10
EN

Stack Overflow用户

发布于 2014-05-29 02:21:02

我只是不得不这样做,而且有一种方法可以做到,尽管这是一种痛苦。您需要将文件作为应用程序的用户帐户进行cat,然后通过管道将其传输到可写位置。这对我运行4.3.3的Nexus 4很有效:

代码语言:javascript
复制
adb shell "run-as org.your.application cat /data/data/org.your.application/your-file > /mnt/sdcard/your-file"
adb pull /mnt/sdcard/your-file
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17883447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档