转载请注明出处: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()); } } }