如何在 Android 中填充测试数据库?

发布于 2024-09-09 16:04:44 字数 190 浏览 9 评论 0原文

我有一个扩展 ProviderTestCase2<> 的测试类。

我想用一些 .db 文件中的数据填充这个测试类数据库。

是否有一些特定的方法可以将某些 .db 文件推送到 ProviderTestCase2 的模拟上下文中?

否则哪种方式更容易从 .db 文件填充数据库?!

非常感谢!!

I have a test class that extends ProviderTestCase2<>.

I would like to populate this test class database with data from some .db files.

Is there some particular method to push some .db file into the Mock Context of a ProviderTestCase2?

Otherwise which way is the easier to populate the database from the .db file?!

Thank you very much!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-09-16 16:04:44

从 SD 卡或类似的东西复制预先存在的 .db 文件怎么样?这是一段快速的代码,可以为您完成此任务:

private void importDBFile(File importDB) {
    String dataDir = Environment.getDataDirectory().getPath();
    String packageName = getPackageName();

    File importDir = new File(dataDir + "/data/" + packageName + "/databases/");
    if (!importDir.exists()) {
        Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
        return;
    }

    File importFile = new File(importDir.getPath() + "/" + importDB.getName());

    try {
        importFile.createNewFile();
        copyDB(importDB, importFile);
        Toast.makeText(this, "Import Successful", Toast.LENGTH_SHORT).show();
    } catch (IOException ex) {
        Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
    }
}

private void copyDB(File from, File to) throws IOException {
    FileChannel inChannel = new FileInputStream(from).getChannel();
    FileChannel outChannel = new FileOutputStream(to).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

希望这适用于您的场景

How about copying in a pre-existing .db file from the SD Card or something similar? This is a quick piece of code that will accomplish this for you:

private void importDBFile(File importDB) {
    String dataDir = Environment.getDataDirectory().getPath();
    String packageName = getPackageName();

    File importDir = new File(dataDir + "/data/" + packageName + "/databases/");
    if (!importDir.exists()) {
        Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
        return;
    }

    File importFile = new File(importDir.getPath() + "/" + importDB.getName());

    try {
        importFile.createNewFile();
        copyDB(importDB, importFile);
        Toast.makeText(this, "Import Successful", Toast.LENGTH_SHORT).show();
    } catch (IOException ex) {
        Toast.makeText(this, "There was a problem importing the Database", Toast.LENGTH_SHORT).show();
    }
}

private void copyDB(File from, File to) throws IOException {
    FileChannel inChannel = new FileInputStream(from).getChannel();
    FileChannel outChannel = new FileOutputStream(to).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }
}

Hopefully this will work for your scenario

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文