Week 6
TableLayout and SQLite
Objectives
 TableLayout, TableRow
 Introduction to SQLite
 SQLite Java classes in Android
 SQLiteOpenHelper
 SQLiteDatabase
 ContentValues
 Cursor
 SQLite Database Tutorial
 Add/Select/Update/Delete
TABLE LAYOUT
TableLayout and TableRow
 Purpose of TableLayout is to organize UI elements in a
table format
 Each row in a TableRow is a TableRow instance
 TableRow is divided into cells, a cell contains a single
child view
 Number of columns is dictated by the row with most
columns
 Width of a column is defined by the widest cell in that
column
 Columns may be set as shrinkable or stretchable (or
both) so they change in size relative to the parent
TableLayout
 A single cell may be configured to span multiple columns
The GUI
The Hierarchical Tree of 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 is written in C, Android SDK provides Java
based wrapper around the DB Interface
 This consists of a set of classes to create and manage DB
Trying SQLite on an AVD
 Android also provides interactive
environment for issuing SQL commands
from within an adb shell session
connected to a running Android AVD
 Once an AVD is running, use Terminal to
connect to the emulator using: adbe
shell
 SQLite databases are actually stored in a
database files on the file system of the
Android device
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
ANDROID SQLITE JAVA
CLASSES
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
 Some key methods of this class are:
 insert(): inserts new row into database table
 delete(): deletes rows from a database table
 query(): performs a query and returns result as
cursor
 execSQL(): executes a SQL, does not return result
 rawQuery()  execute SQL query, returns a cursor
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
 The constructor for the subclass must also be
implemented
 Must call the super class, passing through the application
context, the name of the DB and the DB version.
 Notable methods of SQLiteOpenHelper class:
 getWritableDatabase(): open or creates a DB for reading
and writing. Returns a reference to the DB
 getReadableDatabase(): creates or opens a DB for
reading only. Returns a reference to the DB
 close(): closes the database
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
About the DB example
 This project is a simple data entry and
retrieval designed to add, query and
delete data
 The idea is to allow the tracking of
products
 The DB is: productID.db, which contains
a table named products.
 Each product contains productid,
productname, productquantity columns.
 Productid is PK, and is auto incremented
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
 A database model class to represent the
Product
Create a product class
Select appropriate min SDK
Add a Blank Activity
Input Activity Information
The Data Handler
onCreate and onUpgrade
methods
Add/Load Handler Method
Delete Product Handler Method
Update Product Handler
Method
Main Activity GUI
Main Activity Java Code
Add Activity GUI Using
TableLayout
Add Activity Java Code
Test the Add Feature
Load Activity GUI
Layout for single list item
Load Activity Java Code
Test The Load Feature
Delete/Update GUI
Delete Update Activity Java
Delete Update Activity Java
Test The Delete Feature
Update Activity GUI
Update Activity Java
Test Update Activity
Summaries
 TableLayout, TableRow
 Introduction to SQLite
 SQLite Java classes in Android
 SQLiteOpenHelper
 SQLiteDatabase
 ContentValues
 Cursor
 SQLite Database Tutorial
 Add/Select/Update/Delete