ITEC 222 Module 2.8
ITEC 222 Module 2.8
COURSE MODULE IN
ITEC 222 – Mobile Technology 2
Bachelor of Science in Information Technology
2nd Semester S/Y 2020 – 2021
I. Cover Sheet
A. MODULE NUMBER : 2.8
B. MODULE TITLE : SQLite Databases
C. DURATION : 1 week
D. MODULE PREREQUISITE : None
E. INSTRUCTOR : PHILIP ALGER M. SERRANO
Pretest 00:30
Page 1 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
*Check if accomplished
V. Pretest
Encircle the letter of the correct answer.
2. Which folder does Android store the database in the device’s Internal Storage?
a) public
b) private
c) protected
d) hidden
4. What does the insert() method returns when a new row is created?
a) 0
b) -1
c) 1
d) ID
7. ________ class contains a useful set of APIs for managing your database.
a) DatabaseHelper
b) SQLiteDatabase
c) SQLiteOpenHelper
d) SQLiteHelper
Page 2 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
9. Which Cursor move method is used to move to the next row relative to the current
position?
a) move()
b) moveToFirst()
c) moveToLast()
d) moveToNext()
10. When putting and updating which object is used to create a map of values where the
column names are the keys?
a) ContentValues
b) Cursor
c) SQLiteDatabase
d) SQLiteOpenHelper
Saving data to a database is ideal for repeating or structured data, such as contact information.
This learning module assumes that you are familiar with SQL databases in general and helps you
get started with SQLite databases on Android. The APIs you'll need to use a database on Android
are available in the android.database.sqlite package.
Caution: Although these APIs are powerful, they are fairly low-level and require a great deal of
time and effort to use:
• There is no compile-time verification of raw SQL queries. As your data graph changes,
you need to update the affected SQL queries manually. This process can be time
consuming and error prone.
• You need to use lots of boilerplate code to convert between SQL queries and data
objects.
For these reasons, we highly recommended using the Room Persistence Library as an abstraction
layer for accessing information in your app's SQLite databases.
Define a schema and contract
One of the main principles of SQL databases is the schema: a formal declaration of how the
database is organized. The schema is reflected in the SQL statements that you use to create your
Page 3 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
database. You may find it helpful to create a companion class, known as a contract class,
which explicitly specifies the layout of your schema in a systematic and self-documenting way.
A contract class is a container for constants that define names for URIs, tables, and columns. The
contract class allows you to use the same constants across all the other classes in the same
package. This lets you change a column name in one place and have it propagate throughout
your code.
A good way to organize a contract class is to put definitions that are global to your whole
database in the root level of the class. Then create an inner class for each table. Each inner class
enumerates the corresponding table's columns.
Note: By implementing the BaseColumns interface, your inner class can inherit a primary key field
called _ID that some Android classes such as CursorAdapter expect it to have. It's not required,
but this can help your database work harmoniously with the Android framework.
For example, the following contract defines the table name and column names for a single table
representing an RSS feed:
Once you have defined how your database looks, you should implement methods that create
and maintain the database and tables. Here are some typical statements that create and
delete a table:
Page 4 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Just like files that you save on the device's internal storage, Android stores your database in your
app's private folder. Your data is secure, because by default this area is not accessible to other
apps or the user.
The SQLiteOpenHelper class contains a useful set of APIs for managing your database. When you
use this class to obtain references to your database, the system performs the potentially long-
running operations of creating and updating the database only when needed and not during
app startup. All you need to do is call getWritableDatabase() or getReadableDatabase().
For example, here's an implementation of SQLiteOpenHelper that uses some of the commands
shown above:
Page 5 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Insert data into the database by passing a ContentValues object to the insert() method:
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);
values.put(FeedEntry.COLUMN_NAME_SUBTITLE, subtitle);
// Insert the new row, returning the primary key value of the new row
long newRowId = db.insert(FeedEntry.TABLE_NAME, null, values);
The second argument tells the framework what to do in the event that the ContentValues is empty
(i.e., you did not put any values). If you specify the name of a column, the framework inserts a row
and sets the value of that column to null. If you specify null, like in this code sample, the framework
does not insert a row when there are no values.
The insert() methods returns the ID for the newly created row, or it will return -1 if there was an error
inserting the data. This can happen if you have a conflict with pre-existing data in the database.
To read from a database, use the query() method, passing it your selection criteria and desired
columns. The method combines elements of insert() and update(), except the column list defines
the data you want to fetch (the "projection"), rather than the data to insert. The results of the query
are returned to you in a Cursor object.
SQLiteDatabase db = dbHelper.getReadableDatabase();
Page 6 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
};
The third and fourth arguments (selection and selectionArgs) are combined to create a WHERE
clause. Because the arguments are provided separately from the selection query, they are
escaped before being combined. This makes your selection statements immune to SQL injection.
To look at a row in the cursor, use one of the Cursor move methods, which you must always call
before you begin reading values. Since the cursor starts at position -1,
calling moveToNext() places the "read position" on the first entry in the results and returns whether
or not the cursor is already past the last entry in the result set. For each row, you can read a
column's value by calling one of the Cursor get methods, such as getString() or getLong(). For
each of the get methods, you must pass the index position of the column you desire, which you
can get by calling getColumnIndex() or getColumnIndexOrThrow(). When finished iterating
through results, call close() on the cursor to release its resources. For example, the following shows
how to get all the item IDs stored in a cursor and add them to a list:
Page 7 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
To delete rows from a table, you need to provide selection criteria that identify the rows to
the delete() method. The mechanism works the same as the selection arguments to
the query() method. It divides the selection specification into a selection clause and selection
arguments. The clause defines the columns to look at, and also allows you to combine column
tests. The arguments are values to test against that are bound into the clause. Because the result
isn't handled the same as a regular SQL statement, it is immune to SQL injection.
The return value for the delete() method indicates the number of rows that were deleted from the
database.
Update a database
When you need to modify a subset of your database values, use the update() method.
Updating the table combines the ContentValues syntax of insert() with the WHERE syntax
of delete().
SQLiteDatabase db = dbHelper.getWritableDatabase();
Page 8 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
selectionArgs);
The return value of the update() method is the number of rows affected in the database.
@Override
protected void onDestroy() {
dbHelper.close();
super.onDestroy();
}
The Android SDK includes a sqlite3 shell tool that allows you to browse table contents, run SQL
commands, and perform other useful functions on SQLite databases.
2, 4, 5, 7, 8, 9, 10, 11
References:
• Zack Raskin. 2021. Android Programming For Beginners: Ins And Outs Of Application
Development: Android Programming Handbook.
• Franceschi Hervé. 2018. Android app development, Burlington, MA: Jones et Bartlett
Learning.
• Kristin Marsicano, Bill Phillips, Chris Stewart, and Brian Gardner. 2019. Android
programming: The Big Nerd Ranch guide, Atlanta, GA: Big Nerd Ranch.
• DiMarzio, J. F. 2017. Beginning Android programming with Android Studio. Indianapolis, IN:
Wrox, a Wiley brand.
• Hagos, T. 2018. Learn Android Studio 3: Efficient Android App Development. Berkeley, CA:
Apress.
• Smyth, N. 2018. Android Studio 3.2 development essentials. Place of publication not
identified: Payload Media.
• Ryan Turner. 2018. SQL: the ultimate beginner's guide to learn SQL programming step by
step, United States.
• Google Inc. 2021. Google Maps Platform Google Developers. Retrieved March 2021 from
https://developers.google.com/maps/documentation/
VII. Assignment/Activities
Page 9 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
Assignment 1:
Objective: Do objective 1
Instructions:
For High/Medium level Technology:
Evaluation Criteria
Points
Criteria
10 5
Grammar is
Grammar Grammatically Correct
understandable enough
Total (20)
Page 10 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
2. Which folder does Android store the database in the device’s Internal Storage?
a) public
b) private
c) protected
d) hidden
4. What does the insert() method returns when a new row is created?
a) 0
b) -1
c) 1
d) ID
Page 11 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015
7. ________ class contains a useful set of APIs for managing your database.
a) DatabaseHelper
b) SQLiteDatabase
c) SQLiteOpenHelper
d) SQLiteHelper
9. Which Cursor move method is used to move to the next row relative to the current
position?
a) move()
b) moveToFirst()
c) moveToLast()
d) moveToNext()
10. When putting and updating which object is used to create a map of values where the
column names are the keys?
a) ContentValues
b) Cursor
c) SQLiteDatabase
d) SQLiteOpenHelper
Page 12 of 12