-
Notifications
You must be signed in to change notification settings - Fork 67
Database internal API
This document is deprecated.
The DatabaseHelper class handles interactions with the application database. It deals with 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)
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.
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):
Card card = /* ... */;
getHelper().getCardDao().create(card);
// After this call, card.id is validCard card = getHelper().getCardDao().queryForId(123);
if (card == null) {
// Handle card not found
}Card card = /* ... */;
getHelper().getCardDao().update(card);Card card = /* ... */;
getHelper().getCardDao().delete(card);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) {
}
}long count = getHelper().getCardDao().countOf();