自动备份 SQLite 数据库
Automatically backing up SQLite database
我的应用程序使用了一个小型 SQLite 数据库,我想对其进行备份。我假设这不会自动发生,如果我没有使用 fullBackupContent 对其进行编码,如下所示。如何修改 backupscheme.xml 的内容以正确设置包含路径?我更喜欢在运行时设置数据库位置。
我的backupscheme.xml长得像
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content >
<include domain="database" path="device_info.db"/>
</full-backup-content
我的清单包含:
<application
android:allowBackup="true"
android:icon="@drawable/time_machine_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="mypackage.myapp"
android:fullBackupContent="@xml/backupscheme"
>
<include domain="database" path="device_info.db"/>
这里的域表示解释path
的根目录。 database
映射到默认存储 SQLite 数据库的位置,如果您使用:
getDatabasePath()
在 Context
SQLiteOpenHelper
仅使用普通文件名
openOrCreateDatabase()
只是一个简单的文件名
在这种情况下,文件名应该是您的 path
值。
如果您的数据库出于某种原因存储在其他地方,则 <include>
指令需要进行一些调整。
我的应用程序使用了一个小型 SQLite 数据库,我想对其进行备份。我假设这不会自动发生,如果我没有使用 fullBackupContent 对其进行编码,如下所示。如何修改 backupscheme.xml 的内容以正确设置包含路径?我更喜欢在运行时设置数据库位置。
我的backupscheme.xml长得像
<?xml version="1.0" encoding="utf-8"?>
<full-backup-content >
<include domain="database" path="device_info.db"/>
</full-backup-content
我的清单包含:
<application
android:allowBackup="true"
android:icon="@drawable/time_machine_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="mypackage.myapp"
android:fullBackupContent="@xml/backupscheme"
>
<include domain="database" path="device_info.db"/>
这里的域表示解释path
的根目录。 database
映射到默认存储 SQLite 数据库的位置,如果您使用:
getDatabasePath()
在Context
SQLiteOpenHelper
仅使用普通文件名openOrCreateDatabase()
只是一个简单的文件名
在这种情况下,文件名应该是您的 path
值。
如果您的数据库出于某种原因存储在其他地方,则 <include>
指令需要进行一些调整。