Android SQLite 开发教程(5): 导出数据库到XML 文件

使用SQLiteOpenHelper 创建的数据库为应用程序私有,其路径一般为DATA/data/APP_NAME/databases/FILENAME

  • DATA 为使用Environment.getDataDirectory()返回的路径,一般为你的SD卡的路径。
  • APP_Name为你的应用的名称
  • FILENAME为你的数据库的文件名

其它程序一般无法访问这个文件,因此也给调试带来了不便,当然你可以使用Android SDK 的sqlite3 工具来直接访问这个数据,但个人还是觉的sqlite 使用起来不是十分方便。

你也可以将数据库创建者SD卡上面,此时可以使用SQLiteDatabase 的openOrCreateDatabase 指定要创建的数据库的文件名(指定SD卡上的某个文件名)。

也可以将数据库使用代码复制到SD卡上。 此时可以使用一些桌面系统上的SQLite管理工具,比如Firefox 的SQL Lite manager 插件来访问这个数据库。


一种简洁的方法是将数据库导出到XML文件,下面类DatabaseDump的实现,可以将如何一个数据库所有表和表的内容导出到XML文件中

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
 
class DatabaseDump {
 
 public DatabaseDump(SQLiteDatabase db,String destXml) {
 mDb = db;
 mDestXmlFilename=destXml;
 
 try {
 // create a file on the sdcard to export the
 // database contents to
 File myFile = new File(mDestXmlFilename);
 myFile.createNewFile();
 
 FileOutputStream fOut = new FileOutputStream(myFile);
 BufferedOutputStream bos = new BufferedOutputStream(fOut);
 
 mExporter = new Exporter(bos);
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 
 public void exportData() {
 
 try {
 mExporter.startDbExport(mDb.getPath());
 
 // get the tables out of the given sqlite database
 String sql = "SELECT * FROM sqlite_master";
 
 Cursor cur = mDb.rawQuery(sql, new String[0]);
 cur.moveToFirst();
 
 String tableName;
 while (cur.getPosition() < cur.getCount()) {
 tableName = cur.getString(cur.getColumnIndex("name"));
 
 // don't process these two tables since they are used
 // for metadata
 if (!tableName.equals("android_metadata")
 && !tableName.equals("sqlite_sequence")) {
 exportTable(tableName);
 }
 
 cur.moveToNext();
 }
 mExporter.endDbExport();
 mExporter.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
 
 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();
 }
 
 private String mDestXmlFilename = "/sdcard/export.xml";
 
 private SQLiteDatabase mDb;
 private Exporter mExporter;
 
 class Exporter {
 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 Exporter() throws FileNotFoundException {
 this(new BufferedOutputStream(new FileOutputStream(mDestXmlFilename)));
 
 }
 
 public Exporter(BufferedOutputStream bos) {
 mbufferos = bos;
 }
 
 public void close() throws IOException {
 if (mbufferos != null) {
 mbufferos.close();
 }
 }
 
 public void startDbExport(String dbName) throws IOException {
 String stg = START_DB + dbName + CLOSING_WITH_TICK;
 mbufferos.write(stg.getBytes());
 }
 
 public void endDbExport() throws IOException {
 mbufferos.write(END_DB.getBytes());
 }
 
 public void startTable(String tableName) throws IOException {
 String stg = START_TABLE + tableName + CLOSING_WITH_TICK;
 mbufferos.write(stg.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 {
 String stg = START_COL + name + CLOSING_WITH_TICK + val + END_COL;
 mbufferos.write(stg.getBytes());
 }
 }
 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值