java 在 Android 中将 SQLite 数据库导出为 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2585934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exporting a SQLite database to an XML file in Android
提问by jcrowson
I know this is possible but I'm not really sure where to start. Has anyone been able to achieve this?
我知道这是可能的,但我不确定从哪里开始。有没有人能够做到这一点?
Thanks.
谢谢。
采纳答案by Billy Bob Bain
The DataXmlExporterclass described in this article will export a SQL lite DB to an XML file.
DataXmlExporter本文中描述的类会将 SQL lite DB 导出到 XML 文件。
http://www.screaming-penguin.com/node/7749
http://www.screaming-penguin.com/node/7749
The full example is available in this SVN repo. The ManageDataclass invokes the export.
完整示例可在此 SVN 存储库中找到。在ManageData类调用出口。
http://totsp.com/svn/repo/AndroidExamples/trunk/
http://totsp.com/svn/repo/AndroidExamples/trunk/
You will need to create an application class that exposes the DB and referenced as the application name in the AndroidManifest.xml file. Then use that DB as the argument to the DataXmlExporterconstructor.
您需要创建一个应用程序类来公开数据库并在 AndroidManifest.xml 文件中作为应用程序名称引用。然后使用该数据库作为DataXmlExporter构造函数的参数。
Here's the application class I use. You should already have a class (probably not named DatabaseHelper) that extends SQLiteOpenHelper
这是我使用的应用程序类。你应该已经有一个DatabaseHelper扩展的类(可能没有命名)SQLiteOpenHelper
package com.billybobbain.android.someapp;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
public static final String APP_NAME = "SomeApp";
private DatabaseHelper dataHelper;
@Override
public void onCreate() {
super.onCreate();
Log.d(APP_NAME, "APPLICATION onCreate");
this.dataHelper = new DatabaseHelper(this);
}
@Override
public void onTerminate() {
Log.d(APP_NAME, "APPLICATION onTerminate");
super.onTerminate();
}
public DatabaseHelper getDataHelper() {
return this.dataHelper;
}
public void setDataHelper(DatabaseHelper dataHelper) {
this.dataHelper = dataHelper;
}
}
回答by stuckInOldLodiAgain
Have a look at the source code here exporting-a-sqlite-database-to-an-xml-file-in-android
看看这里的源代码exporting-a-sqlite-database-to-an-xml-file-in-android
The only change I had to make (to stop a few Eclipse warnings) was to close a cursor in the exportData( ) method. To make the code more portable, I also passed the XML file and location as an argument rather then as a declared final field.
我必须做的唯一更改(停止一些 Eclipse 警告)是关闭 exportData() 方法中的游标。为了使代码更具可移植性,我还将 XML 文件和位置作为参数传递,而不是作为声明的最终字段。
The code writes the XML file to the SD card. Now, @mmaitlen who listed the source code on his blog doesn't add in any features to test for the existence of an external storage unit. So that's left for you to do.
该代码将 XML 文件写入 SD 卡。现在,在他的博客上列出源代码的@mmaitlen 没有添加任何功能来测试外部存储单元的存在。所以剩下的就交给你了。
However, you can embed some simple code to test for the existence of a writeable memory card with the following snippet (untested):
但是,您可以嵌入一些简单的代码来使用以下代码段(未经测试)测试可写存储卡是否存在:
sdOkToWrite = false;
String sdTest = Environment.getExternalStorageState();
if (sdTest.equals(Environment.MEDIA_MOUNTED)) {
sdOkToWrite = true;
} else {
// Here's where you can code what to do without the external storage
}
Testing for the external storage is useful if you have large files to create that may exceed internal capacity.
如果您要创建可能超过内部容量的大文件,则测试外部存储非常有用。
回答by IHeartAndroid
I found this very helpful:
我发现这很有帮助:
http://www.phonesdevelopers.com/1788273/
http://www.phonesdevelopers.com/1788273/
Using it the following way to export it to the sd card:
使用以下方式将其导出到 SD 卡:
File sd = Environment.getExternalStorageDirectory();
String path = sd + "/" + DB_NAME + ".xml";
DatabaseDump databaseDump = new DatabaseDump(mySQLiteOpenHelperObject.getReadableDatabase(), path);
databaseDump.exportData();
Of course don't forget:
当然不要忘记:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

