导入导出到android sqlite数据库

导入导出到android sqlite数据库,第1张

概述我看过几篇关于如何在android中导入和导出数据库的帖子,我找到了这些代码,但我似乎无法使它工作.我收到错误java.io.filenotfoundexception/storage/sdcard0/BackupFolder/DatabaseName:打开失败的ENOENT(没有这样的文件或目录).我改变了一些东西,但我仍然没有找到文件异常

我看过几篇关于如何在android中导入和导出数据库的帖子,我找到了这些代码,但我似乎无法使它工作.我收到错误java.io.filenotfoundexception / storage / sdcard0 / BackupFolder / Databasename:打开失败的ENOENT(没有这样的文件或目录).我改变了一些东西,但我仍然没有找到文件异常

这是我的出口:

private voID exportDB() {        try {             db.open();             file newfile = new file("/sdcard/myexport");            inputStream input = new fileinputStream(            "/data/data/com.example.mycarfuel/databases/MyDatabase");                OutputStream output = new fileOutputStream(newfile);                byte[] buffer = new byte[1024];                int length;                while ((length = input.read(buffer)) > 0) {                    output.write(buffer, 0, length);                }                output.flush();                output.close();                input.close();                db.close();            } catch (fileNotFoundException e1) {                // Todo auto-generated catch block                e1.printstacktrace();            } catch (IOException e) {                // Todo auto-generated catch block                e.printstacktrace();            }}

和我的导入:

private voID importDB() {        try {            file sd = Environment.getExternalStorageDirectory();            file data = Environment.getDataDirectory();            if (sd.canWrite()) {                String currentDBPath = "//data//" + "Packagename"                        + "//databases//" + "Databasename";                    String backupDBPath = "/BackupFolder/Databasename";                file backupDB = new file(data, currentDBPath);                file currentDB = new file(sd, backupDBPath);                fileChannel src = new fileinputStream(currentDB).getChannel();                fileChannel dst = new fileOutputStream(backupDB).getChannel();                dst.transferFrom(src, 0, src.size());                src.close();                dst.close();                Toast.makeText(getBaseContext(), backupDB.toString(),Toast.LENGTH_LONG).show();        }    } catch (Exception e) {        Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)                .show();    }}

解决方法:

sqlite数据库到我们的本地文件系统 –

功能声明 –

        try {            backupDatabase();        } catch (IOException e1) {            // Todo auto-generated catch block            e1.printstacktrace();        }

功能定义 –

public static voID backupDatabase() throws IOException {        //Open your local db as the input stream        String infilename = "/data/data/com.myapp.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();    }
总结

以上是内存溢出为你收集整理的导入/导出到android sqlite数据库全部内容,希望文章能够帮你解决导入/导出到android sqlite数据库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101300.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存