Saltar a contenido
For the complete DHIS2 documentation index, see llms.txt.

Database

Database architecture

The SDK uses Android Room Persistence Library as its database layer. Room provides compile-time verification of SQL queries, reduces boilerplate code, and offers better integration with Android architecture components.

The database implementation is internal to the SDK and apps should interact with data through the provided repositories and services. Direct database access is not recommended as the internal structure may change between versions.

Database scope

The SDK keeps the data of a [server, user] pair in an isolated database. As of version 1.6.0, the SDK supports multiple accounts (pairs [server, user]) and the information for each account is stored in an isolated database. The database is deleted only when the account is deleted. Databases are created automatically on a successful login.

Encryption

As of SDK version 1.1.0, it is possible to store the data in an encrypted database. The encryption key is generated randomly by the SDK and kept secure. The encryption status (if the database is encrypted or not) can be configured at server level in the android-settings-app. The default status is false: If the app is not installed, the database won't be encrypted.

Durante el primer inicio de sesión para un servidor y usuario determinados, el estado de cifrado se descargará desde la API y se creará una base de datos del tipo dado.

En inicios de sesión posteriores o sincronizaciones de metadatos, el SDK volverá a descargar el estado de cifrado desde el servidor y, si se cambia, cifrará o descifrará la base de datos actual sin pérdida de datos.

Rendimiento de cifrado

  • Tamaño de la base de datos: el tamaño de la base de datos es aproximadamente el mismo, independientemente de que esté cifrada o no.
  • Velocidad: las lecturas y escrituras son en promedio de un 5 a un 10% más lentas si se usa una base de datos cifrada.

Import / export

The database can be exported and imported in a different device.

One of the main use cases of this functionality is debugging: sometimes it is hard to know the reason for a sync problem or a bug, and it is very useful to replicate the issue in an emulator or a different device.

// Export database
val database = d2.maintenanceModule().databaseImportExport().exportLoggedUserDatabase()

// Import database
val metadata = d2.maintenanceModule().databaseImportExport().importDatabase(database)

// The metadata object contains information about the database (serverUrl, username,...) 

// Once the database is imported, it is possible to login as usual
d2.userModule().login("username", "password", "serverUrl")

The export process encrypts the database using ZIP encryption, so the database file can't be read unless the right user credentials are provided.

Things to consider: - The exported file only contains the database, it does not contain file resources (images, icons, files,...). - The receiver device must run an SDK version that is equal or higher than the SDK version used to export the database.