备份Android后如何还原Sqlite数据库

编程入门 行业动态 更新时间:2024-10-26 00:21:31
本文介绍了备份Android后如何还原Sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我搜索了很多有关备份/还原Sqlite数据库的信息,我发现了将sqlite文件复制到SD卡的代码,这是代码

I searched a lot about backup/restore Sqlite database i found code to copy sqlite file to SD card this is the code

private void exportDB() { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + "<package name>" + "//databases//" + "<db name>"; String backupDBPath = "<destination>"; File currentDB = new File(data, currentDBPath); File backupDB = 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(getApplicationContext(), "Backup Successful!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT) .show(); } }

现在我在SD卡中有数据库文件(.db),我想将数据还原到应用程序,我尝试了此代码,但它不是将数据还原到应用程序

Now i have database file (.db) in SD card and i want to restore data to app i tried this code but it is not restore data to app

private void importDB() { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + "<package name>" + "//databases//" + "<database name>"; String backupDBPath = "<backup db filename>"; // From SD directory. File backupDB = new File(data, currentDBPath); File currentDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(backupDB).getChannel(); FileChannel dst = new FileOutputStream(currentDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(getApplicationContext(), "Import Successful!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT) .show(); } }

我的问题是如何将sqlite数据库还原到我的应用程序?

My question is how i can restore sqlite database to my app?

推荐答案

这是有效的数据库还原的核心代码(来自 try 中的if (dbfile ..).

This is the core code of a working DB restore (from if (dbfile .. in a try).

private static final int BUFFERSZ = 32768; private byte[] buffer = new byte[BUFFERSZ]; ........ dbfile = new File(currentdbfilename); ....... if (dbfile.delete()) { origdeleted = true; } FileInputStream bkp = new FileInputStream(backupfilename); OutputStream restore = new FileOutputStream(currentdbfilename); copylength = 0; while ((copylength = bkp.read(buffer)) > 0) { restore.write(buffer, 0, copylength); } restore.flush(); restore.close(); restoredone = true; bkp.close();

主要区别在于我删除了数据库文件并使用写操作而不是传输操作.稍后在成功还原后,我还使用以下方法重新启动该应用程序(可能有点过头,但对我有用),因为您可以获得无法预料的结果(我认为原始数据库的某些部分可能会从内存/缓存数据中访问):

The main differences are that I delete the DB file and use writes rather than transfers. Later and upon a successful restore I also use the following to restart the App (might be overkill but it works for me) as you can get unpredictable results (I think parts of the original database may be accessed from memory/cached data):-

Intent i = getBaseContext().getPackageManager() .getLaunchIntentForPackage( getBaseContext().getPackageName() ); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); finish(); startActivity(i); System.exit(0);

更多推荐

备份Android后如何还原Sqlite数据库

本文发布于:2023-10-24 17:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524555.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:备份   数据库   Android   Sqlite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!