Přeskočit obsah
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.

Během prvního přihlášení pro daný server a uživatele bude stav šifrování stažen z API a bude vytvořena databáze daného typu.

V pozdějších přihlášeních nebo synchronizaci metadat SDK znovu stáhne stav šifrování ze serveru a pokud se změní, zašifruje nebo dešifruje aktuální databázi bez ztráty dat.

Výkon šifrování

  • Velikost databáze: velikost databáze je přibližně stejná, bez ohledu na to, zda je šifrována nebo ne.
  • Rychlost: čtení a zápis jsou pomocí šifrované databáze v průměru o 5 až 10% pomalejší.

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.