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

时间:2017-02-08 15:41:30

标签: java android sqlite android-studio export

我正在尝试在我的Android应用程序中实现导出和导入,我已经尝试了下面的导出,但它不起作用,并没有给出任何错误。有人可以帮帮我。

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();
}

我按这样调用方法:

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

我做错了什么?并且一旦导出我怎样才能导回数据?

1 个答案:

答案 0 :(得分:-1)

我相信你需要的东西在这里得到解答......它对我有用..

copy database file to sdcard in android

注意:这并不是一个懒惰的答案,简单地说,我知道这有效,所以希望它能帮助海报!