简体   繁体   English

如何在android studio中导出和导入数据库sqlite?

[英]How to export and import database sqlite in android studio?

I am trying to implement an export and import in my android app, I have tried the below for export but it isn't working and doesn't give any errors.我正在尝试在我的 android 应用程序中实现导出和导入,我尝试了以下导出但它不起作用并且没有给出任何错误。 Could someone please help me out.有人可以帮我吗。

public static void export() throws IOException {
    //Open your local db as the input stream
    String inFileName = "/data/data/com.example.main/databases/myDB";
    File dbFile = new File(inFileName);
    FileInputStream fis = new FileInputStream(dbFile);

    String outFileName = Environment.getExternalStorageDirectory()+"/myDB";
    //Open the empty db as the output stream
    OutputStream output = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = fis.read(buffer))>0){
        output.write(buffer, 0, length);
    }
    //Close the streams
    output.flush();
    output.close();
    fis.close();
}

and I call the method like so:我像这样调用方法:

try {
    export();
}
catch (IOException e) {
    e.printStackTrace();
}

What am I doing wrong?我究竟做错了什么? and also once exported how can I import the data back?并且一旦导出,如何将数据导入回来?

I believe that what you require is answered here... it worked for me..我相信你需要的在这里得到了回答......它对我有用......

copy database file to sdcard in android 在android中将数据库文件复制到sdcard

Note: This isn't intended to be a lazy answer, simply, I know this works so hopefully it will help the poster!注意:这不是一个懒惰的答案,简单地说,我知道这是有效的,所以希望它会对海报有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM