Skip to content

Database internal API

Daniel Underhay edited this page Jun 24, 2018 · 24 revisions

Warning

This document is deprecated.


The DatabaseHelper class handles interactions with the application database. It deals with Card objects.

Card Objects

These represent a single card, and include:

  • ID (used for database purposes)
  • Name (editable)
  • CardData (see here) (editable - through set card data dialog (TODO))
  • Date created
  • Date acquired
  • Location acquired (editable - through map UI (TODO))
  • Notes (editable)
  • Tags (editable) (TODO)
  • Photo (editable) (TODO)

DatabaseHelper

The DatabaseHelper is a ORMLite wrapper around Android's SQLite database functionality.

An Activity that uses DatabaseHelper must do so either by extending from OrmLiteBaseAppCompatActivity and using getHelper() to get an instance of the DatabaseHelper.

Actions

CRUD and associated actions are supported through the Data Access Objects (DAOs) the DatabaseHelper exposes. ORMLite has summary and complete documentation on the use of ORMLite DAOs.

To make this more concrete, here's a summary of Card object specific usage through its DAO (given by getCardDao):

Add card to database (Create)

Card card = /* ... */;
getHelper().getCardDao().create(card);
// After this call, card.id is valid

Find card in database (Read)

Card card = getHelper().getCardDao().queryForId(123);
if (card == null) {
    // Handle card not found
}

Update card in database (Update)

Card card = /* ... */;
getHelper().getCardDao().update(card);

Delete card from database (Delete)

Card card = /* ... */;
getHelper().getCardDao().delete(card);

Enumerate cards in database

Taking the important caveat given in the ORMLite documentation about not exiting while iterating over a DAO in mind, we can do the following (ugh):

CloseableWrappedIterable<Card> wrappedIterable = null;
try {
    wrappedIterable = getHelper().getCardDao().getWrappedIterable();
    for (Card card : wrappedIterable) {
        // ...
    }
} finally {
    try {
        if (wrappedIterable != null)
            wrappedIterable.close();
    } catch (IOException e) {
    }
}

Get count of cards in database

long count = getHelper().getCardDao().countOf();