Recipe Creating a Separate Database Package

A good modular structure to classes is essential for more complicated Android projects. Here, the database class is put in its own package com.cookbook.data so it is easy to reuse. This package contains three classes: MyDB, MyDBhelper, and Constants.

The MyDB class is shown in Listing 9.9. It contains a SQLiteDatabase instance and a MyDBhelper class (described in the following) with the methods that follow:

■ MyDB()—Initializes a MyDBhelper instance (the constructor).

■ open()—Initializes a SQLiteDatabase instance using the MyDBhelper.This opens a writeable database connection. If SQLite throws any exception, it tries to get a readable database instead.

■ close()—Closes the database connection.

■ insertdiary()—Saves a diary entry to the database as name-value pairs in a ContentValues instance, and then passes the data to the SQLitedatabase instance to do an insert.

■ getdiaries()—Reads the diary entries from the database, saves them in a Cursor class, and returns it from the method.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment