14
SQLite Databases

Almost every application needs a place to save data for the long term, longer than savedInstanceState will keep it around. Android provides a place to do this for you: a local filesystem on your phone or tablet’s flash memory storage.

Each application on an Android device has a directory in the device’s sandbox. Keeping files in the sandbox protects them from being accessed by other applications or even the prying eyes of users (unless the device has been “rooted,” in which case the user can get to whatever he or she likes).

Each application’s sandbox directory is a child of the device’s /data/data directory named after the application package. For CriminalIntent, the full path to the sandbox directory is /data/data/com.bignerdranch.android.criminalintent.

However, most application data is not stored in plain old files. Here is why: Say that you had a file with all of your Crimes written out. To change the title on a Crime at the beginning of the file, you would have to read in the entire file and write out a whole new version. With a lot of Crimes, that would take a long time.

This is where SQLite comes in. SQLite is an open source relational database, like MySQL or PostgreSQL. Unlike other databases, though, SQLite stores its data in simple files, which you can read and write using the SQLite library. Android includes this SQLite library in its standard library, along with some additional Java helper classes.

This chapter will not cover everything SQLite. For that, you will want to visit www.sqlite.org, which has complete documentation of SQLite itself. Here you will see how Android’s basic SQLite helper classes work. These will let you open, read, and write to SQLite databases in your application sandbox without necessarily knowing where that is.

Defining a Schema

Before you create a database, you have to decide what will be in that database. CriminalIntent stores a single list of crimes, so you will define one table named crimes (Figure 14.1).

Figure 14.1  The crimes table

Screenshot shows crimes table in a database.

People do this kind of thing in a lot of different ways in the programming world. They are all trying to achieve the same thing: to DRY up their code. DRY means Don’t Repeat Yourself and refers to a rule of thumb when writing a program: If you write something down, write it down in one authoritative place. That way, instead of repeating yourself all over the place, you are always referring to the one authoritative place for that information.

Doing this with databases can be involved. There are even complex tools called object-relational mappers (ORMs) that let you use your model objects (like Crime) as your One True Definition. In this chapter, you will take the simpler route of defining a simplified database schema in Java code that says what your table is named and what its columns are.

Start by creating a class to put your schema in. You will call this class CrimeDbSchema, but in the Create New Class dialog, enter database.CrimeDbSchema. This will put the CrimeDbSchema.java file in its own database package, which you will use to organize all your database-related code.

Inside CrimeDbSchema, define an inner class called CrimeTable to describe your table.

Listing 14.1  Defining CrimeTable (CrimeDbSchema.java)

public class CrimeDbSchema {
    public static final class CrimeTable {
        public static final String NAME = "crimes";
    }
}

The CrimeTable class only exists to define the String constants needed to describe the moving pieces of your table definition. The first piece of that definition is the name of the table in your database, CrimeTable.NAME.

Next, describe the columns.

Listing 14.2  Defining your table columns (CrimeDbSchema.java)

public class CrimeDbSchema {
    public static final class CrimeTable {
        public static final String NAME = "crimes";

        public static final class Cols {
            public static final String UUID = "uuid";
            public static final String TITLE = "title";
            public static final String DATE = "date";
            public static final String SOLVED = "solved";
        }
    }
}

With that, you will be able to refer to the column named title in a Java-safe way: CrimeTable.Cols.TITLE. That makes it much safer to update your program if you ever need to change the name of that column or add additional data to the table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.151.231