Chapter 10. Beyond database basics

 

If you’ve been developing Android applications, you may have used a database to persist information. In this chapter, we’ll cover some advanced tips for developers who are familiar with using databases in Android.

Hack 41 Building databases with ORMLite: Android v2.2+ Contribute- ed by William Sanville

Android applications usually have a requirement for some form of persistent storage, meaning data that’s saved between each time a user runs the application. To facilitate this need, Android ships with a relational database called SQLite. This hack covers creating an entire database instance using a tool called ORMLite, an Object-Relational Mapping (ORM) tool, as well as reading and writing data.

Our end goal is to create an application that displays articles broken down in categories and allows users to comment on each article. The finished application can be seen in figure 41.1.

Figure 41.1. Finished application

All database operations in this application are performed using ORMLite, rather than writing any SQL statements by hand. This approach can save time by reducing the amount of code needed to create the database schema.

41.1. A simple data model

Hack 42 Creating custom functions in SQLite: Android v1.6+

Hack 43 Batching database operations: Android v2.1+