Android and SQLite
Android and SQLite
Objectives
TableLayout, TableRow
Introduction to SQLite
SQLite Java classes in Android
SQLiteOpenHelper
SQLiteDatabase
ContentValues
Cursor
TABLE LAYOUT
The GUI
SQLITE
Introduction
The use of database is an essential
aspect of most applications
Ranging from applications entirely data
driven, to those simply need to store
small amounts of data
Persistent data storage is more
important taking into transient lifecycle
of an Android activity
Android provides SQLite bounded with
Android OS, provides to facilitate
persistent storage
What is SQLite?
SQLite is an embedded, RDBMS
Most RDBMS are standalone server processes run
independently (e.g., Ms. SQL, Oracle, MySQL, etc)
SQLite is referred to as embedded because it is
provided in the form of a library that is linked into
applications.
No standalone DB server running in the background
All DB operations are handled internally within the
application through calls to functions of SQLite Lib
SQLite DB path
By default, the file system path for the
/data/data/<package
name>/databases/<database filename>.db
DB file
is
E.g., in an app with package com.exmple.MyDBApp
/data/data/com.example.MyDBApp/databases/mydatabase.db
Cursor
Provided to access to the results of a database
query
Its use to step through each record then access to
data fields using various methods
Some key methods are:
close(): release resources and close this cursor
getCount(): returns number of rows in the result
moveToFirst(): moves to first row in the result
moveToLast(): moves to the last row in the result
moveToNext(): moves to the next row in the result
move(): moves by a specified offset from current position
get<Type>() returns value of specified <Type>
SQLiteDatabase
It provides the primary interface between the
application code and underlying SQLite
databases
To create/delete databases
To perform SQL based operations on databases
SQLiteOpenHelper
Designed to make it easier to create and update
databases
It must be subclassed within the code of the
application seeking database access
Following call back methods must be implemented
onCreate()
Called when database is created for the first time.
Its passed as argument an SQLiteDatabase object newly
created
This is ideal location to initialize DB like creating table and
inserting initial rows
onUpgrade()
Called when app code contains more recent DB version
Typically happen when app is updated on the device
SQLiteOpenHelper
In addition to the mandatory callback methods
(listed previously), there are some more methods
onOpen(): called when DB is opened
ContentValues
ContentValues is a convenience class
that allows key/value pairs to be
declared
Consisting of table column identifiers
and the values to be stored in each
column
This class is of particular use when
inserting or updating entries in a
database table
SQLITE DATABASE
TUTORIAL
The classes
Besides the Activity class we will need
A database handler
A subclass of SQLiteOpenHelper
Provides an abstract layer between the DB
and activity
Delete/Update GUI
Summaries
TableLayout, TableRow
Introduction to SQLite
SQLite Java classes in Android
SQLiteOpenHelper
SQLiteDatabase
ContentValues
Cursor