所以我的应用程序使用内容提供程序和SQLite数据库,类似于Google记事本示例。我发现SimpleXML并希望使用它来导入/导出数据库,以便它可以在设备之间传输。我找到的唯一例子是example. 我想不必写一些中间类来传入/传出数据库。有没有人找到一个简单的方法,甚至可能没有simplexml?
答案 0 :(得分:2)
您是需要导入/导出整个数据库还是只导出部分数据库。对于应该传输完整数据库的情况,您可能只是复制数据库文件本身,该文件通常存储在/ data / data / YOUR_APP / databases
中答案 1 :(得分: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);
}
}
}
试试这个。这对我有用。