java - 以编程方式下载 SQLite 文件

标签 java android sqlite

在我的 Android 应用程序中,我使用 SQLite 数据库。 我需要在应用程序中有一个选项来下载数据库文件。

因为有时我需要查看那里的数据,所以用户会下载文件并将其发送给我,我将使用 SQLite 浏览器浏览它。

可行吗?如果是的话怎么办?

   String src = "/data/data/myPackage/databases/Mydb.db";
   String dest = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();

copyFileOrDirectory(src , dest);

 public static void copyFileOrDirectory(String srcDir, String dstDir) {

        try {
            File src = new File(srcDir);
            File dst = new File(dstDir, src.getName());

                copyFile(src, dst);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

最佳答案

通常,SQLite 数据库位于 /data/data/your.applications.package/databases/database.db 路径中。

所以,你可以使用该路径;不过,我建议您通过以下方式获取数据库路径:

File dbFile = getDatabasePath("dbname");
String dbPath = dbFile.getPath();

然后,您可以将此文件夹中的数据库复制到您想要的文件夹中。 将数据库复制到Downloads可以模拟SQLite数据库的“下载”。

关于java - 以编程方式下载 SQLite 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59751190/

相关文章:

java - 如何在android中构建CLM

java - Neo4j 嵌入式数据库忽略事务日志限制

android - OnItemSelectedListener 不适用于微调器

python - sqlite3 python 查询包含多个变量错误

java - 将 List<String> 传递给 Play Framework 中的 Jquery

java - 为什么在使用 StackTraceElement 时 getLineNumber 返回 -1

android - 获取正在运行的进程列表并终止特定进程

android - 如何隐藏和显示一些 ImageButtons?

android - 获取 Assets 文件夹中的 SQLite 路径

Android 联系人管理器