備份Sqlite DB到XML文件:

前端之家收集整理的这篇文章主要介绍了備份Sqlite DB到XML文件:前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载请注明出处:http://blog.csdn.net/krislight

项目中遇到备份与还原App数据的需求,需要把DB数据备份到一个XML文件中,然后保存到SD卡上,还原的时候直接从XML文件解析数据进行insert DB动作。

现总结下实现方法,定义一个工具类

  1. /**
  2. * from sqlite to xml
  3. *
  4. * @author Kris
  5. */
  6. public class DatabaseLog {
  7.  
  8. // back up dir
  9. private String mDestXmlFileDir = "/psmd/appName/backup/";
  10. private sqliteDatabase mDb;
  11. private Exporter mExporter;
  12.  
  13. /**
  14. * @param db database
  15. */
  16. public DatabaseLog(sqliteDatabase db,String fileName) {
  17. mDb = db;
  18. try {
  19. Calendar cal = Calendar.getInstance();
  20. //use date+time as filename e.g. 2013_04_03_13_23_49.backup
  21. if(TextUtils.isEmpty(fileName)){
  22. fileName = cal.get(Calendar.YEAR)+"_"+cal.get(Calendar.MONTH)+"_"+cal.get(Calendar.DAY_OF_MONTH)+"_"
  23. +cal.get(Calendar.HOUR_OF_DAY)+"_"+cal.get(Calendar.MINUTE)+"_"+cal.get(Calendar.MINUTE);
  24. }
  25. fileName += ".backup";
  26. File sdDir = Environment.getExternalStorageDirectory();
  27. File theDir = new File(sdDir,mDestXmlFileDir);
  28. if (!theDir.exists()) {
  29. theDir.mkdirs();
  30. }
  31. File picFile = new File(theDir,fileName);
  32. picFile.createNewFile();
  33. FileOutputStream fOut = new FileOutputStream(picFile);
  34. BufferedOutputStream bos = new BufferedOutputStream(fOut);
  35. mExporter = new Exporter(bos);
  36. } catch (FileNotFoundException e) {
  37. e.printStackTrace();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42.  
  43. /**
  44. * export
  45. */
  46. public void exportData() {
  47. try {
  48. //1.先查找icon存放的目录复制所有图片到備份目錄下面
  49. // 还原的时候检查这个栏位对应的文件是否存在,不存在则用backup下面的文件路径,然后update db
  50. mExporter.startDbExport(C_9510_PSMD_DBAdapter.DATABASE_NAME);
  51. //TODO export database and table
  52. exportTable("C_EVNT_EVNT");
  53. exportTable("C_EVNT_PSON");
  54. exportTable("R_EVNT_SEND");
  55. exportTable("R_EVNT_RECV");
  56. exportTable("C_TMPL_TYPE");
  57. exportTable("C_TMPL_EVDS");
  58. mExporter.endDbExport();
  59. mExporter.close();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  1. 转载请注明出处:http://blog.csdn.net/krislight
  2.  
  3. /**
  4. *
  5. * @param tableName
  6. * @throws IOException
  7. */
  8. private void exportTable(String tableName) throws IOException {
  9. mExporter.startTable(tableName);
  10. // get everything from the table
  11. String sql = "select * from " + tableName;
  12. Cursor cur = mDb.rawQuery(sql,new String[0]);
  13. int numcols = cur.getColumnCount();
  14. cur.moveToFirst();
  15. // move through the table,creating rows // and adding each column with
  16. // name and value
  17. // to the row
  18. while (cur.getPosition() < cur.getCount()) {
  19. mExporter.startRow();
  20. String name;
  21. String val;
  22. for (int idx = 0; idx < numcols; idx++) {
  23. name = cur.getColumnName(idx);
  24. val = cur.getString(idx);
  25. mExporter.addColumn(name,val);
  26. }
  27. mExporter.endRow();
  28. cur.moveToNext();
  29. }
  30. cur.close();
  31. mExporter.endTable();
  32. }
  33.  
  34. public class Exporter {
  35. private static final String OPEN_XML_STANZA = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  36. private static final String CLOSING_WITH_TICK = "'>";
  37. private static final String START_DB = "<export-database name='";
  38. private static final String END_DB = "</export-database>";
  39. private static final String START_TABLE = "<table name='";
  40. private static final String END_TABLE = "</table>";
  41. private static final String START_ROW = "<row>";
  42. private static final String END_ROW = "</row>";
  43. private static final String START_COL = "<col name='";
  44. private static final String END_COL = "</col>";
  45. private BufferedOutputStream mbufferos;
  46.  
  47. public String getFileName() {
  48. return "";
  49.  
  50. }
  51.  
  52. public Exporter(BufferedOutputStream bos) {
  53. mbufferos = bos;
  54. }
  55.  
  56. public void close() throws IOException {
  57. if (mbufferos != null) {
  58. mbufferos.close();
  59. }
  60. }
  61.  
  62. public void startDbExport(String dbName) throws IOException {
  63. StringBuffer sb = new StringBuffer();
  64. sb.append(OPEN_XML_STANZA).append(START_DB).append(dbName).append(CLOSING_WITH_TICK);
  65. mbufferos.write(sb.toString().getBytes());
  66. }
  67.  
  68. public void endDbExport() throws IOException {
  69. mbufferos.write(END_DB.getBytes());
  70. }
  71.  
  72. public void startTable(String tableName) throws IOException {
  73. StringBuffer sb = new StringBuffer();
  74. sb.append(START_TABLE).append(tableName).append(CLOSING_WITH_TICK);
  75. mbufferos.write(sb.toString().getBytes());
  76. }
  77.  
  78. public void endTable() throws IOException {
  79. mbufferos.write(END_TABLE.getBytes());
  80. }
  81.  
  82. public void startRow() throws IOException {
  83. mbufferos.write(START_ROW.getBytes());
  84. }
  85.  
  86. public void endRow() throws IOException {
  87. mbufferos.write(END_ROW.getBytes());
  88. }
  89.  
  90. public void addColumn(String name,String val) throws IOException {
  91. StringBuffer sb = new StringBuffer();
  92. sb.append(START_COL).append(name).append(CLOSING_WITH_TICK).append(val).append(END_COL);
  93. mbufferos.write(sb.toString().getBytes());
  94. }
  95. }
  96. }

猜你在找的Sqlite相关文章