Exporting a SQLite database to an XML file in Android

后端 未结 3 1524
别跟我提以往
别跟我提以往 2020-12-15 11:51

I know this is possible but I\'m not really sure where to start. Has anyone been able to achieve this?

Thanks.

3条回答
  •  无人及你
    2020-12-15 12:06

    Have a look at the source code here 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.

    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.

    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.

提交回复
热议问题