当前位置:编程学习 > XML/UML >>

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文件中

[java]
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()); 
 } 
 } 
  

 


补充:移动开发 , Android ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,