查看外部设备中数据库的数据表

时间:2012-08-30 17:36:32

标签: android database sqlite device adb

在外部设备中获取和可视化我的数据库数据表的唯一方法是在外部设备中分配超级用户权限的权限?不存在允许在模拟器中可视化数据表的另一种方式吗?

我提出这个问题是因为这种超级用户权限不会激发我的安全感。

感谢您的关注(PS:抱歉错误,但英语不是我的母语:))

1 个答案:

答案 0 :(得分:0)

只需让您的应用程序复制文件,即可添加将数据库文件从内部只读应用程序存储区导出到SD卡的功能。

然后用你所拥有的任何方式从那里获得它。适用于任何设备,无需root。

private void exportDb() {
    File database = getDatabasePath("myDb.db");
    File sdCard = new File(Environment.getExternalStorageDirectory(), "myDb.db");
    if (copy(database, sdCard)) {
        Toast.makeText(this, "Get db from " + sdCard.getPath(), Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this, "Copying the db failed", Toast.LENGTH_LONG).show();
    }
}

private static boolean copy(File src, File target) {
    // try creating necessary directories
    target.mkdirs();
    boolean success = false;
    FileOutputStream out = null;
    FileInputStream in = null;
    try {
        out = new FileOutputStream(target);
        in = new FileInputStream(src);
        byte[] buffer = new byte[8 * 1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        success = true;
    } catch (FileNotFoundException e) {
        // maybe log
    } catch (IOException e) {
        // maybe log
    } finally {
        close(in);
        close(out);
    }
    if (!success) {
        // try to delete failed attempts
        target.delete();
    }
    return success;
}

private static void close(final Closeable closeMe) {
    if (closeMe != null)
        try {
            closeMe.close();
        } catch (IOException ignored) {
            // ignored
        }
}