您好我一直在尋找如何將我的數據庫導出爲XML或文本文件,但似乎無法找到我在找什麼。我遇到的所有教程和代碼snipets似乎都不能解釋我在找什麼。有沒有人知道任何教程或網站,說明我應該如何在這個時尚出口?導出數據庫文件爲文本或xml在android中
2
A
回答
2
1
public class DataXmlExporter extends DataBaseDemoActivity {
static final String DATASUBDIRECTORY = "bookwormdata";
// private final SQLiteDatabase db;
private XmlBuilder xmlBuilder;
Button butt;
/* public DataXmlExporter(final SQLiteDatabase db) {
this.db = db;
}*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main7);
butt=(Button)findViewById(R.id.exportxml);
String exportFileNamePrefix=exportxml;
// public void export(final String dbName, final String exportFileNamePrefix) throws IOException {
Log.i(Constants._COUNT, "exporting database - " + dbname + " exportFileNamePrefix=" + exportFileNamePrefix);
try {
xmlBuilder = new XmlBuilder();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmlBuilder.start(dbname);
// get the tables
String sql = "select * from dept";
Cursor c = db.rawQuery(sql, new String[0]);
if (c.moveToFirst()) {
//while (c.moveToNext()) {
String tableName ="dept";//c.getString(c.getColumnIndex("dep_id"));
// skip metadata, sequence, and uidx (unique indexes)
if (!tableName.equals("android_metadata") && !tableName.equals("sqlite_sequence")
&& !tableName.startsWith("uidx")) {
try {
exportTable(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }
}
String xmlString = null;
try {
xmlString = xmlBuilder.end();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
writeToFile(xmlString, exportFileNamePrefix + ".xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(Constants._COUNT, "exporting database complete");
Toast.makeText(DataXmlExporter.this, "DB xml backup Successfully", 2000).show();
butt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(DataXmlExporter.this, DataBaseDemoActivity.class);
startActivity(intent);
}
});
}
private void exportTable(final String tableName) throws IOException {
xmlBuilder.openTable(tableName);
String sql = "select * from " + tableName;
Cursor c = db.rawQuery(sql, new String[0]);
if (c.moveToFirst()) {
int cols = c.getColumnCount();
do {
xmlBuilder.openRow();
for (int i = 0; i < cols; i++) {
/*if(i==6)
{
//String id = c.getString(c.getColumnIndex("photo"));
String str = new String(image);
xmlBuilder.addColumn(c.getColumnName(i), str);
}*/
xmlBuilder.addColumn(c.getColumnName(i), c.getString(i));
}
xmlBuilder.closeRow();
} while (c.moveToNext());
}
c.close();
xmlBuilder.closeTable();
}
private void writeToFile(final String xmlString, final String exportFileName) throws IOException {
File dir = new File(Environment.getExternalStorageDirectory(), DataXmlExporter.DATASUBDIRECTORY);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, exportFileName);
file.createNewFile();
ByteBuffer buff = ByteBuffer.wrap(xmlString.getBytes());
FileChannel channel = new FileOutputStream(file).getChannel();
try {
channel.write(buff);
} finally {
if (channel != null) {
channel.close();
}
}
}
/**
* XmlBuilder is used to write XML tags (open and close, and a few attributes)
* to a StringBuilder. Here we have nothing to do with IO or SQL, just a fancy StringBuilder.
*
* @author ccollins
*
*/
static class XmlBuilder {
private static final String OPEN_XML_STANZA = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
private static final String CLOSE_WITH_TICK = "'>";
private static final String DB_OPEN = "<database name='";
private static final String DB_CLOSE = "</database>";
private static final String TABLE_OPEN = "<table name='";
private static final String TABLE_CLOSE = "</table>";
private static final String ROW_OPEN = "<row>";
private static final String ROW_CLOSE = "</row>";
private static final String COL_OPEN = "<col name='";
private static final String COL_CLOSE = "</col>";
private final StringBuilder sb;
public XmlBuilder() throws IOException {
sb = new StringBuilder();
}
void start(final String dbName) {
sb.append(XmlBuilder.OPEN_XML_STANZA);
sb.append(XmlBuilder.DB_OPEN + dbName + XmlBuilder.CLOSE_WITH_TICK);
}
String end() throws IOException {
sb.append(XmlBuilder.DB_CLOSE);
return sb.toString();
}
void openTable(final String tableName) {
sb.append(XmlBuilder.TABLE_OPEN + tableName + XmlBuilder.CLOSE_WITH_TICK);
}
void closeTable() {
sb.append(XmlBuilder.TABLE_CLOSE);
}
void openRow() {
sb.append(XmlBuilder.ROW_OPEN);
}
void closeRow() {
sb.append(XmlBuilder.ROW_CLOSE);
}
void addColumn(final String name, final String val) throws IOException {
sb.append(XmlBuilder.COL_OPEN + name + XmlBuilder.CLOSE_WITH_TICK + val + XmlBuilder.COL_CLOSE);
}
}
}
試試這個。這對我有用。
+0
是DataBaseDemoActivity,你已經擴展了這個活動,你的DBAdapter類? – Dhaval 2013-11-20 13:40:10
相關問題
- 1. Android - 在文本文件中導出數據庫
- 2. 導出 - 文本文件 - SQL數據庫
- 3. 將SQLite數據庫導出到Android中的XML文件中
- 4. 將數據庫中的數據導出到XML文件
- 5. 將數據庫導出爲CSV文件
- 6. 如何導出sqlite數據庫爲android中的csv文件?
- 7. 將Iibre office數據庫表導出到文本文件或csv文件
- 8. 使用Python將Oracle數據庫表導出爲XML文件?
- 9. 導出SQLite數據庫到csv文件在android中
- 10. 保存XML文件或數據庫上的文本
- 11. 將文本文件導入數據庫
- 12. 在Android中讀取文本文件或XML文件java
- 13. 將XML文件導入PostgreSQL數據庫
- 14. ASP.NET MVC導入XML文件數據庫
- 15. 導出SQL數據,文本文件
- 16. 將數據導出到文本文件
- 17. MSSqlServer2000數據導出到文本文件?
- 18. 導入csv或文本文件到數據庫
- 19. 導入文本文件到MS Excel或SQL Server CE數據庫
- 20. 將數據存儲在文本文件或xml中
- 21. 用文本文件中的數據導出文本來自R
- 22. 作爲一個XML文件導出數據在谷歌appengine
- 23. Android:如何將XML文件導出(或導入)到Dropbox?
- 24. 如何將數據庫的備份導出到文本文件
- 25. 在android中點擊按鈕時數據庫導出爲xls文件?
- 26. 只能將數據庫,文本文件或xml文件添加到ASP.net項目
- 27. Oracle:將Oracle數據庫導出爲.sql文件並在Android中導入與SQLite相同的.sql文件db
- 28. 在xml文件中管理數據庫
- 29. 在數據庫中存儲XML文件
- 30. 更喜歡數據庫或文件在Android中的數據
嘿,如果這個答案幫助你,然後接受它... – Shruti 2012-10-19 08:44:11