Hello Database

To demonstrate SQLite, let's create a little application called Events that stores records in a database and displays them later. We're going to start simple and build up from there. Open a new "Hello, Android" program using these values in the project wizard:

Project name: Events Build Target: Android 2.2 Application name: Events

Package name: org.example.events Create Activity: Events Min SDK Version: 8

As always, you can download the complete source code from the book's website.

We need somewhere to hold a few constants describing the database, so let's create a Constants interface:

Download Eventsv1/src/org/example/events/Constants.java

package org.example.events;

import android.provider.BaseColumns;

public interface Constants extends BaseColumns { public static final String TABLE_NAME = "events";

// Columns in the Events database public static final String TIME = "time";

public static final String TITLE = "title";

Each event will be stored as a row in the events table. Each row will have an _id, time, and title column. _id is the primary key, declared in the BaseColumns interface that we extend. time and title will be used for a time stamp and event title, respectively.

0 0

Post a comment