是否有可能数据库文件,保存到SD卡上?有可能、卡上、数据库文件、SD

2023-09-04 05:41:18 作者:噢,惟一鹿季

可能重复:   是否有可能数据库文件复制到SD卡上?

我有我的Andr​​oid手机的数据库,我需要得到的信息到SD卡上。

I have a database on my Android phone, and I need to get the information onto an SD card.

是否有可能在数据库文件到SD卡保存在一个可读的状态呢?我一直没能找到有关如何做到这一点的任何信息。我知道数据库的名称,和字段等...

Is it possible to save the database file onto the SD card in a readable state? I haven't been able to find any information on how to do this. I know the name of the database, and fields etc...

我发现,展示如何保存到SD卡的一些例子,但不是我需要的到底是什么。

I've found some examples that show how to save to SD cards, but not exactly what I need.

某些源$ C ​​$ c表示数据库文件复制到SD卡将是完美的。

Some source code that copies the database file to an SD card would be perfect.

希望这个问题是很清楚。

Hopefully this question is clear enough.

推荐答案

是的。下面是我使用的功能:

Yes. Here is the function that i use:

public void copyDBToSDCard() {
    try {
        InputStream myInput = new FileInputStream("/data/data/com.myproject/databases/"+DATABASE_NAME);

        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                Log.i("FO","File creation failed for " + file);
            }
        }

        OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        Log.i("FO","copied");

    } catch (Exception e) {
        Log.i("FO","exception="+e);
    }


}

有关我工作的一个项目,我把在主屏幕的菜单选项,我可以随时从调用这个函数。然后,我将数据库移动到我的桌面,并使用SQLite的经理插件Firefox打开它。

For a project that I worked on, I put a menu option in the home screen that I could call this function from at any time. Then, I'd move the database to my desktop and open it up with the SQLite Manager plugin for FireFox.