Lecture 5 & 6-Android Data persistence
Lecture 5 & 6-Android Data persistence
Introduction
Android SQLite
• Android supports SQLite, which provides
a relational database for a mobile device
• i.e., it contains tables (consisting of
rows & columns), indexes, etc. that
form a “schema”
www.drdobbs.com/database/using-sqlite-on-android/232900584
5
Android Persistent Data Storage
Android SQLite
• Android supports SQLite, which provides
a relational database for a mobile device
• It’s designed to operate within a
small footprint (~350kB) within
a single cross-platform disk file
en.wikipedia.org/wiki/SQLite
6
Android Persistent Data Storage
Android SQLite
• Android supports SQLite, which provides
a relational database for a mobile device
• It’s designed to operate within a
small footprint (<300kB) within
a single cross-platform disk file
• Implements most of SQL92 &
supports so-called “ACID”
transactions
• Atomic, Consistent, Isolated, & Durable
en.wikipedia.org/wiki/SQL-92
7
Android Persistent Data Storage
Android SQLite
• Android supports SQLite, which provides
a relational database for a mobile device
• It’s designed to operate within a
small footprint (<300kB) within
a single cross-platform disk file
• Implements most of SQL92 &
supports so-called “ACID”
transactions
• Access to an SQLite database typically
involves accessing the Android filesystem
• Database operations are typically
asynchronous since filesystem access
can be slow
• e.g., access is often made via AsyncTask,
AsyncQueryHandler, CursorLoader, etc.
www.vogella.com/articles/AndroidSQLite/article.html
8
Android Persistent Data Storage
SQLiteDatabase
• SQLiteDatabase is the base class
for working with a SQLite database
in Android
• It provides the insert(),
update(), & delete() methods
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
9
Android Persistent Data Storage
SQLiteDatabase
• SQLiteDatabase is the base class
for working with a SQLite database
in Android
• It provides the insert(),
update(), & delete() methods
• It also provides the execSQL()
method that can execute an
SQL statement directly
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
10
Android Persistent Data Storage
SQLiteDatabase
• SQLiteDatabase is the base class
for working with a SQLite database
in Android
• Queries can be created via
rawQuery() & query() methods or
via the SQLiteQueryBuilder class
developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html
11
Android Persistent Data Storage
ContentValues
• The ContentValues object is used
by SQLiteDatabase to define
key/values
• The “key” represents the table
column identifier & the “value”
represents the content for the
table record in this column
developer.android.com/reference/android/content/ContentValues.html
12
Android Persistent Data Storage
ContentValues
• The ContentValues object is used
by SQLiteDatabase to define
key/values
• ContentValues can be used for
inserts & updates of database
entries
developer.android.com/reference/android/content/ContentValues.html
13
Android Persistent Data Storage
SQLiteOpenHelper
• Recommended means of using
SQLiteDatabase is to subclass the
SQLiteOpenHelper class
• In constructor call the super()
method of SQLiteOpenHelper,
specifying database name &
current database version
developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
14
Android Persistent Data Storage
SQLiteOpenHelper
• Recommended means of using
SQLiteDatabase is to subclass the
SQLiteOpenHelper class
• In constructor call the super()
method of SQLiteOpenHelper,
specifying database name &
current database version
• Override onCreate(), which is
called by SQLite if the database
does not yet exist
• e.g., execute CREATE TABLE
command
developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
15
Android Persistent Data Storage
SQLiteOpenHelper
• Recommended means of using
SQLiteDatabase is to subclass the
SQLiteOpenHelper class
• In constructor call the super()
method of SQLiteOpenHelper,
specifying database name &
current database version
• Override onCreate(), which is
called by SQLite if the database
does not yet exist
• Override onUpgrade(), which is
called if the database version
increases in App code to allow
database schema updates
developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
16
Android Persistent Data Storage
SQLiteOpenHelper
• Recommended means of using
SQLiteDatabase is to subclass the
SQLiteOpenHelper class
• Use SQLiteOpenHelper methods to
open & return underlying database
• e.g., getReadableDatabase() &
getWriteableDatabase() to
access an SQLiteDatabase
object either in read or write
mode, respectively
developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
17
Android Persistent Data Storage
db = dbHelper.getWritableDatabase();
insertArtists();
deleteLadyGaga(); Create a read/write databse
Cursor c = readArtists();
displayArtists(c);
}
... Perform various operations
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
20
#insert(java.lang.String, java.lang.String, android.content.ContentValues)
Android Persistent Data Storage
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
21
#insert(java.lang.String, java.lang.String, android.content.ContentValues)
Android Persistent Data Storage
22
Android Persistent Data Storage
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
23
#delete(java.lang.String, java.lang.String, java.lang.String[])
Android Persistent Data Storage
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
24
#delete(java.lang.String, java.lang.String, java.lang.String[])
Android Persistent Data Storage
return db.delete("artists",
"name" + "=?",
new String []
{"Lady Gaga"}); Note the use of
} the “whereArgs”
25
Android Persistent Data Storage
sql the SQL query. The SQL string must not be ; terminated
selectionArgs You may include ?’s in where clause in the query, which are
replaced by the values from selectionArgs (the values will be
bound as Strings)
Returns
A Cursor object, which is positioned before the first entry
Returns
27 the first entry
A Cursor object, which is positioned before
Android Persistent Data Storage
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
28
#rawQuery(java.lang.String, java.lang.String[])
Android Persistent Data Storage
developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
#query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[],
java.lang.String, java.lang.String,
29 java.lang.String)
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• This allows buffering of query
results efficiently since all data
needn’t be loaded into memory
developer.android.com/reference/android/database/Cursor.html
30
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
developer.android.com/reference/android/database/Cursor.html
31
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
• moveToFirst() & moveToNext()
move between individual data rows
developer.android.com/reference/android/database/Cursor.html
32
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
• moveToFirst() & moveToNext()
move between individual data rows
• isAfterLast() checks if the end of
the query result has been reached
developer.android.com/reference/android/database/Cursor.html
33
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
• moveToFirst() & moveToNext()
move between individual data rows
• isAfterLast() checks if the end of
the query result has been reached
• Provides typed get*() methods
• e.g., getLong(columnIndex) &
getString(columnIndex) to access
column data for current position
of result
developer.android.com/reference/android/database/Cursor.html
34
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
• moveToFirst() & moveToNext()
move between individual data rows
• isAfterLast() checks if the end of
the query result has been reached
• Provides typed get*() methods
• Provides getColumnIndexOrThrow
(String) to get column index for a
column name of table
developer.android.com/reference/android/database/Cursor.html
35
Android Persistent Data Storage
Cursor Iterators
• Query() returns a Cursor Iterator
that represents result of a query &
points to one row of query result
• getCount() returns # of elements
of the resulting query
• moveToFirst() & moveToNext()
move between individual data rows
• isAfterLast() checks if the end of
the query result has been reached
• Provides typed get*() methods
• Provides getColumnIndexOrThrow
(String) to get column index for a
column name of table
• Must be closed via close()
developer.android.com/reference/android/database/Cursor.html
36
Android Persistent Data Storage
developer.android.com/reference/android/widget/SimpleCursorAdapter.html
37
Android Persistent Data Storage
38
Android Persistent Data Storage
39
Android Persistent Data Storage
Summary
40
Android Persistent Data Storage
Summary
developer.android.com/guide/topics/providers/content-providers.html
44
Developing Android Apps with Eclipse
developer.android.com/guide/topics/providers/content-providers.html
45
Developing Android Apps with Eclipse
developer.android.com/guide/topics/providers/content-providers.html
46
Developing Android Apps with Eclipse
developer.android.com/guide/topics/providers/content-providers.html
47
Developing Android Apps with Eclipse
developer.android.com/reference/android/provider/package-summary.html
48
Developing Android Apps with Eclipse
49
Developing Android Apps with Eclipse
50
Developing Android Apps with Eclipse
Overview of ContentResolver
• ContentProvider never accessed
directly, but accessed indirectly
via a ContentResolver
• ContentProvider not created
until a ContentResolver tries
to access it
developer.android.com/reference/android/content/ContentResolver.html
51
Developing Android Apps with Eclipse
Overview of ContentResolver
• ContentProvider never accessed
directly, but accessed indirectly
via a ContentResolver
• ContentResolvers manage &
support ContentProviders
• Enables use of ContentProviders
across multiple Apps
developer.android.com/reference/android/content/ContentResolver.html
52
Developing Android Apps with Eclipse
Overview of ContentResolver
• ContentProvider never accessed
directly, but accessed indirectly
via a ContentResolver
• ContentResolvers manage &
support ContentProviders
• Enables use of ContentProviders
across multiple Apps
• Provides additional services, such
as change notification & IPC
developer.android.com/reference/android/content/ContentResolver.html
53
Developing Android Apps with Eclipse
Overview of ContentResolver
• ContentProvider never accessed
directly, but accessed indirectly
via a ContentResolver
• ContentResolvers manage &
support ContentProviders
• Context.getContentResolver()
accesses default ContentResolver
ContentResolver cr =
getContentResolver();
developer.android.com/reference/android/content/ContentResolver.html
54
Developing Android Apps with Eclipse
www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html
55
Developing Android Apps with Eclipse
www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html
56
Developing Android Apps with Eclipse
www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html
57
Developing Android Apps with Eclipse
www.androiddesignpatterns.com/2012/06/content-resolvers-and-content-providers.html
58
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
59
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
• e.g., content://authority/path/id
• content - data is managed by a
ContentProvider
60
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
• e.g., content://authority/path/id
• content - data is managed by a
ContentProvider
• authority – id for the content provider
61
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
• e.g., content://authority/path/id
• content - data is managed by a
ContentProvider
• authority – id for the content provider
• path – 0 or more segments indicating
the type of data to access
62
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
• e.g., content://authority/path/id
• content - data is managed by a
ContentProvider
• authority – id for the content provider
• path – 0 or more segments indicating
the type of data to access
• id – specific record being requested
63
Developing Android Apps with Eclipse
Content URIs
• Any URI that begins with the content://
scheme represents a resource served up
by a Content Provider
• e.g., content://authority/path/id
• ContentProviders are a façade that
offers data encapsulation via Content
Uri objects used as handles
• The data could be stored in a SQLite
database, in flat files, retrieved off a
device, be stored on some server
accessed over the Internet, etc.
64
Developing Android Apps with Eclipse
developer.android.com/reference/android/content/ContentProvider.html
65
#insert(android.net.Uri, android.content.ContentValues)
Developing Android Apps with Eclipse
developer.android.com/reference/android/content/ContentProvider.html
66
#delete(android.net.Uri, java.lang.String, java.lang.String[])
Developing Android Apps with Eclipse
developer.android.com/reference/android/content/ContentProvider.html
67
#applyBatch(java.util.ArrayList<android.content.ContentProviderOperation>)
Developing Android Apps with Eclipse
Querying a ContentResolver
• Use ContentResolver. query()
to retrieve data
• Returns a Cursor instance
for accessing results
• A Cursor is an iterator over
a result set
developer.android.com/reference/android/content/ContentProvider.html#query
68 java.lang.String[], java.lang.String)
(Uri, java.lang.String[], java.lang.String,
Developing Android Apps with Eclipse
69
Developing Android Apps with Eclipse
Summary
• A SQLite database is private to the
App which creates it
• If you want to share data with other
App you can use a content provider
70
Developing Android Apps with Eclipse
Summary
• A SQLite database is private to the
App which creates it
• A content provider allows App to
access data
• In most cases this data is stored
in an SQlite database
71
Developing Android Apps with Eclipse
Summary
• A SQLite database is private to the
App which creates it
• A content provider allows App to
access data
• While a content provider can be used
within an App to access data, its is
typically used to share data with other
App
72
Developing Android Apps with Eclipse
Summary
• A SQLite database is private to the
App which creates it
• A content provider allows App to
access data
• While a content provider can be used
within an App to access data, its is
typically used to share data with other
App
• App data is by default private, so a
content provider is a convenient to
share you data with other application
based on a structured interface
73
Developing Android Apps with Eclipse
Summary
• A SQLite database is private to the
App which creates it
• A content provider allows App to
access data
• While a content provider can be used
within an App to access data, its is
typically used to share data with other
App
• App data is by default private, so a
content provider is a convenient to
share you data with other application
based on a structured interface
• A content provider must be declared
in the AndroidManifest.xml file
74