Accessing and modifying data

For this exercise, we are using a SQLite database to store our data. This is useful if only your application will need to access or modify the data. If you wish for other activities to access or modify the data, you have to expose the data using a ContentProvider. If you are interested, you can find out more about content providers or the whole subject of Data Storage. The NotePad sample in the samples / folder of the SDK also has an example of how to create a ContentProvider.

Take a look at the NotesDbAdapter class — this class is provided to encapsulate data access to a SQLite database that will hold our notes data and allow us to update it.

At the top of the class are some constant definitions that will be used in the application to look up data from the proper field names in the database. There is also a database creation string defined, which is used to create a new database schema if one doesn't exist already.

Our database will have the name data, and have a single table called notes, which in turn has three fields: id, title and body. The id is named with an underscore convention used in a number of places inside the Android SDK and helps keep a track of state. The id usually has to be specified when querying or updating the database (in the column projections and so on). The other two fields are simple text fields that will store data.

The constructor for NotesDbAdapter takes a Context, which allows it to communicate with aspects of the Android operating system. This is quite common for classes that need to touch the Android system in some way. The Activity class implements the Context class, so usually you will just passthis from your Activity, when needing a Context.

The open() method calls up an instance of DatabaseHelper, which is our local implementation of the SQLiteOpenHelper class. It callsgetWritableDatabase() , which handles creating/opening a database for us.

close() just closes the database, releasing resources related to the connection.

createNote() takes strings for the title and body of a new note, then creates that note in the database. Assuming the new note is created successfully, the method also returns the row id value for the newly created note.

deleteNote() takes a rowIdfor a particular note, and deletes that note from the database.

fetchAllNotes() issues a query to return a Cursor over all notes in the database. The query() call is worth examination and understanding. The first field is the name of the database table to query (in this case DATABASE TABLE is "notes"). The next is the list of columns we want returned, in this case we want the id, title and body columns so these are specified in the String array. The remaining fields are, in order: selection, selectionArgs, groupBy, having and orderBy. Having these all null means we want all data, need no grouping, and will take the default order. See SQLiteDatabase for more details.

Note: A Cursor is returned rather than a collection of rows. This allows Android to use resources efficiently -- instead of putting lots of data straight into memory the cursor will retrieve and release data as it is needed, which is much more efficient for tables with lots of rows.

fetchNote() is similar to fetchAllNotes() but just gets one note with the rowIdwe specify. It uses a slightly different version of the SQLiteDatabase query() method. The first parameter (settrue) indicates that we are interested in one distinct result. The selection parameter (the fourth parameter) has been specified to search only for the row "where _id =" the rowId we passed in. So we are returned a Cursor on the one row.

And finally, updateNote() takes a rowId, title and body, and uses a ContentValues instance to update the note of the given rowId.

0 0

Post a comment