Skip to content

Extending SQLite Db Classes

Ryan Fischbach edited this page Feb 8, 2016 · 1 revision

If you are using the database.ProviderContract, ProviderDatabase, and ProviderService classes to not only create your own database, but to create a multi-layered stack of extensible databases that all reside on the same provider, then there is a few things to keep in mind when creating/extending such a stack.

*Contract, *Database, and *Provider are a triplet. You cannot override one without the others.

Minimum Extension Requirements

  • In your extended *Contract class, you need to override the mDbContract static property to be an instance of your new class.
  • You need to copy the old static{} statement that sets up the mDbInfo static property that sets the authority prefix, MIME subtype prefix, and the database's filename to what you desire in your app.
  • Your extended *Contract needs to override getDbName() so that the android:authorities="x" attribute will match your authority prefix + "." + the results of getDbName() in your AndroidManifest.xml.
  • Your *Provider descendent needs to specify your new *Contract class in the getDbContract() overridden method.

Things to Keep in Mind When Stacking

  • Table names must be unique to avoid SQLite stack collisions trying to create different schemas for the same table.
  • Inner class names must be unique to avoid stack compiler collisions.
  • RowVars are useful means for abstracting many simple table functions to get/update/insert single rows.
  • Database onUpgrade() is only a single integer; so maintaining each stack separately based on a single integer is challenging. Ensure that when you finally need to write upgrade code that you double check all the stacks you descend from and write a case statement for each upgrade your descendant(s) may have had since the last time you checked.

Clone this wiki locally