Introducing SQLite.
Introducing SQLite.
By providing functionality through a library, rather than as a separate process, each database becomes an integrated part of the
application that created it. This reduces external dependencies, minimizes latency, and simplifi es transaction locking and
synchronization.
SQLite has a reputation of being extremely reliable and is the database system of choice for many consumer electronic devices,
including several MP3 players, the iPhone, and the iPod Touch.
Lightweight and powerful, SQLite differs from many conventional database engines by using a loosely typed approach to column
defi nitions. Rather than requiring column values to conform to a single type, the values in each row for each column are
individually typed. As a result, there’s no strict type checking when assigning or extracting values from each column within a
row.
For more comprehensive coverage of SQLite, including its particular strengths and limitations, check out the official site at
www.sqlite.org/.
Queries in Android are returned as Cursor objects. Rather than extracting and returning a copy of the result values, Cursors act
as pointers to a subset of the underlying data. Cursors are a managed way of controlling your position (row) in the result set of a
database query.
The Cursor class includes several functions to navigate query results including, but not limited to, the following:
❑ moveToFirst Moves the cursor to the fi rst row in the query result.
❑ moveToNext Moves the cursor to the next row.
❑ moveToPrevious Moves the cursor to the previous row.
❑ getCount Returns the number of rows in the result set.
❑ getColumnIndexOrThrow Returns an index for the column with the specifi ed name (throwing an exception if no column
exists with that name).
❑ getColumnName Returns the name of the specifi ed column index.
❑ getColumnNames Returns a String array of all the column names in the current cursor.
❑ moveToPosition Moves the cursor to the specifi ed row.
❑ getPosition Returns the current cursor position.
Android provides a mechanism to manage Cursor resources within your Activities. The startManagingCursor method integrates
the Cursor’s lifetime into the parent Activity’s lifetime management. When you’ve fi nished with the Cursor, call
stopManagingCursor to do just that. Later in this chapter, you’ll learn how to query a database and how to extract specifi c
row/column values from the resulting Cursor objects.