備份Sqlite DB到XML文件:
转载请注明出处:http://blog.csdn.net/krislight
项目中遇到备份与还原App数据的需求,需要把DB数据备份到一个XML文件中,然后保存到SD卡上,还原的时候直接从XML文件解析数据进行insert DB动作。
现总结下实现方法,定义一个工具类
/**
* from sqlite to xml
*
* @author Kris
*/
public class DatabaseLog { // back up dir
private String mDestXmlFileDir = "/psmd/appName/backup/";
private SQLiteDatabase mDb;
private Exporter mExporter; /**
* @param db database
*/
public DatabaseLog(SQLiteDatabase db,String fileName) {
mDb = db;
try {
Calendar cal = Calendar.getInstance();
//use date+time as filename e.g. 2013_04_03_13_23_49.backup
if(TextUtils.isEmpty(fileName)){
fileName = cal.get(Calendar.YEAR)+"_"+cal.get(Calendar.MONTH)+"_"+cal.get(Calendar.DAY_OF_MONTH)+"_"
+cal.get(Calendar.HOUR_OF_DAY)+"_"+cal.get(Calendar.MINUTE)+"_"+cal.get(Calendar.MINUTE);
}
fileName += ".backup";
File sdDir = Environment.getExternalStorageDirectory();
File theDir = new File(sdDir, mDestXmlFileDir);
if (!theDir.exists()) {
theDir.mkdirs();
}
File picFile = new File(theDir, fileName);
picFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(picFile);
BufferedOutputStream bos = new BufferedOutputStream(fOut);
mExporter = new Exporter(bos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* export
*/
public void exportData() {
try {
//1.先查找icon存放的目录复制所有图片到備份目錄下面
// 还原的时候检查这个栏位对应的文件是否存在,不存在则用backup下面的文件路径,然后update db
mExporter.startDbExport(C_9510_PSMD_DBAdapter.DATABASE_NAME);
//TODO export database and table
exportTable("C_EVNT_EVNT");
exportTable("C_EVNT_PSON");
exportTable("R_EVNT_SEND");
exportTable("R_EVNT_RECV");
exportTable("C_TMPL_TYPE");
exportTable("C_TMPL_EVDS");
mExporter.endDbExport();
mExporter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
转载请注明出处:http://blog.csdn.net/krislight /**
*
* @param tableName
* @throws IOException
*/
private void exportTable(String tableName) throws IOException {
mExporter.startTable(tableName);
// get everything from the table
String sql = "select * from " + tableName;
Cursor cur = mDb.rawQuery(sql, new String[0]);
int numcols = cur.getColumnCount();
cur.moveToFirst();
// move through the table, creating rows // and adding each column with
// name and value
// to the row
while (cur.getPosition() < cur.getCount()) {
mExporter.startRow();
String name;
String val;
for (int idx = 0; idx < numcols; idx++) {
name = cur.getColumnName(idx);
val = cur.getString(idx);
mExporter.addColumn(name, val);
}
mExporter.endRow();
cur.moveToNext();
}
cur.close();
mExporter.endTable();
} public class Exporter {
private static final String OPEN_XML_STANZA = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
private static final String CLOSING_WITH_TICK = "'>";
private static final String START_DB = "<export-database name='";
private static final String END_DB = "</export-database>";
private static final String START_TABLE = "<table name='";
private static final String END_TABLE = "</table>";
private static final String START_ROW = "<row>";
private static final String END_ROW = "</row>";
private static final String START_COL = "<col name='";
private static final String END_COL = "</col>";
private BufferedOutputStream mbufferos; public String getFileName() {
return ""; } public Exporter(BufferedOutputStream bos) {
mbufferos = bos;
} public void close() throws IOException {
if (mbufferos != null) {
mbufferos.close();
}
} public void startDbExport(String dbName) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append(OPEN_XML_STANZA).append(START_DB).append(dbName).append(CLOSING_WITH_TICK);
mbufferos.write(sb.toString().getBytes());
} public void endDbExport() throws IOException {
mbufferos.write(END_DB.getBytes());
} public void startTable(String tableName) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append(START_TABLE).append(tableName).append(CLOSING_WITH_TICK);
mbufferos.write(sb.toString().getBytes());
} public void endTable() throws IOException {
mbufferos.write(END_TABLE.getBytes());
} public void startRow() throws IOException {
mbufferos.write(START_ROW.getBytes());
} public void endRow() throws IOException {
mbufferos.write(END_ROW.getBytes());
} public void addColumn(String name, String val) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append(START_COL).append(name).append(CLOSING_WITH_TICK).append(val).append(END_COL);
mbufferos.write(sb.toString().getBytes());
}
}
}
備份Sqlite DB到XML文件:的更多相关文章
- XML文件编码问题
这两天的过程中的一个项目,以解决编码格式ANSI的xml当文件.我遇到了一些问题.下面的例子现在将总结分析过程. 通过win7记事本或notepad++创建一个xml文件test_source: &l ...
- Java眼中的XML文件写入
创建DOM方式生成XML文档 DOMTest package com.imooc.domtest.test; import java.io.File; import java.io.IOExcepti ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- android开发 解析服务器端xml文件数据存储到android客户端SQLite数据库
以下面xml文件为例对其解析(假设此xml就在服务器端Server项目下的servlet包下的MenuServlet文件的输出流中): <?xml version="1.0" ...
- Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈
目录截图: 1.activity_main.xml 主界面效果: <?xml version="1.0" encoding="utf-8"?> &l ...
- android读取xml文件来实现省份,城市,区的选择
本博客如需转载.请注明出处. ------------------------------------------------------------------------------------- ...
- 流暢的pyhton4---數據庫備份
一.linux數據庫備份腳本 1.数据库备份,命令如下: ./pg_dump -h localhost -p 5432 -U postgres -W -F c -b -v -f "/opt/ ...
- java读取xml文件
public ArrayList getMessage(){ String xmlFileName = null; List list = new ArrayList(); MessageBean m ...
- java解析XML文件
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...
随机推荐
- Windows 7 SP1 x64 旗舰版 微软官方安装U盘的制作
[ 本主题由 中山艹泥喵 于 2013-08-20 23:14:33 设为精华1,原因:不错~ ] 最后由 风中枯萎 于 2015-12-15 17:44:15 修改 安装Windows 7操作系统主 ...
- a标签的href="javascript:void(0)"和href="#"的区别
修正一个说法上的bug吧.对于IE6来说,点击后gif暂停bug仅仅发生在“javascript:伪协议未加分号”的情形下. 我再来提供一个视角吧. 给<a>标签增加href属性,就意味着 ...
- 13_输出映射1_resultType
输出映射主要有两种:resultType和resultMap [resultType] 可以返回三种类型 pojo对象:例如select * from user where id=? pojo对象列表 ...
- PAT_1026 程序运行时间
问题描述: 要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock ti ...
- 学习笔记---C++析构函数心得
1.动态分配的对象的析构函数 class man{ public: man(){ cout<<"man begin"<<endl; }; ~man(){ c ...
- ajax跨域访问 webservice
前端代码 $.ajax({ type: "POST", url: "http://localhost:9767/WebService1.asmx/HelloWorld?j ...
- ajax的访问 WebService 的方法
如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name= "HttpPo ...
- js点击 密码输入框密码显示隐藏
很多密码框都有个眼睛标记,点击能显示密码.原理就是点击切换password为text等显示 下面上代码 <!DOCTYPE html> <html> <head> ...
- 默认时,销毁会话,session_unset, session_destory
<?php /** 一般我们登录时,开启了会话,就会自动生成 session 有关的文件, 保存有相关的用户登录信息,所以正常情况下得退出登录, 同时也要清空 session 有关的文件和相关的 ...
- [转载] PowerMokito 使用
一.为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等). 而我们没法控制这些外部依赖的对象,为了解 ...