0% found this document useful (0 votes)
115 views

ITEC 222 Module 2.8

The document is a course module that discusses using SQLite databases on Android. It covers defining a database schema using a contract class to specify table and column names. It also discusses using the SQLiteOpenHelper class to create and manage the database, including creating tables with SQL statements and storing the database privately on the device. The goal is to provide an introduction to using SQLite databases to save structured data on Android.

Uploaded by

Leamae Sebullen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views

ITEC 222 Module 2.8

The document is a course module that discusses using SQLite databases on Android. It covers defining a database schema using a contract class to specify table and column names. It also discusses using the SQLiteOpenHelper class to create and manage the database, including creating tables with SQL statements and storing the database privately on the device. The goal is to provide an introduction to using SQLite databases to save structured data on Android.

Uploaded by

Leamae Sebullen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Republic of the Philippines

Camarines Sur Polytechnic Colleges


Nabua, Camarines Sur
ISO 9001:2015

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

II. Module Objectives (Desired Learning Outcomes)


1. Distinguish different SQL Queries

III. Vocabulary List


• SQLite – is a C-language library that implements a small, fast, self-contained, high-reliability,
full-featured, SQL database engine. It is built into all mobile phones and most computers
and comes bundled inside countless other applications that people use every day.
• Database – a structured set of data held in a computer, especially one that is accessible
in various ways.

IV. Learning Plan


ACTIVITY DURATION REMARKS*

Pretest 00:30

Study Objective 1, Information Sheet 1: SQLite Database 03:00

Do Objective 1 & 2, Assignment/Activity 1 01:00

Do Objective 1 & 2, Posttest/Quiz 1 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.

1. Which API package is needed to use database on Android?


a) database.android.sqlite
b) sqlite.database.android
c) android.sqlite.database
d) android.database.sqlite

2. Which folder does Android store the database in the device’s Internal Storage?
a) public
b) private
c) protected
d) hidden

3. What method is used to read information for a database?


a) query()
b) insert()
c) update()
d) delete()

4. What does the insert() method returns when a new row is created?
a) 0
b) -1
c) 1
d) ID

5. What is the return value of the delete() method?


a) # of Rows Deleted
b) # of Columns Deleted
c) # of Rows Inserted
d) # of Rows Affected

6. What is the return value of the update() method?


a) # of Rows Deleted
b) # of Columns Deleted
c) # of Rows Inserted
d) # of Rows Affected

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

8. The result of an SQLite query is returned as a _____ object.


a) Row
b) Cursor
c) Result
d) Column

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

VI. Information Sheet


Information Sheet 1: SQLite Database
Saving data using SQLite

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:

public final class FeedReaderContract {


// To prevent someone from accidentally instantiating the contract class,
// make the constructor private .
private FeedReaderContract() {}

/* Inner class that defines the table contents */


public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
}
}

Create a database using an SQL helper

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:

private static final String SQL_CREATE_ENTRIES =


"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_TITLE + " TEXT," +
FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";

private static final String SQL_DELETE_ENTRIES =

Page 4 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015

"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;

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().

Note: Because they can be long-running, be sure that you


call getWritableDatabase() or getReadableDatabase() in a background thread.

To use SQLiteOpenHelper, create a subclass that overrides


the onCreate() and onUpgrade() callback methods. You may also want to implement
the onDowngrade() or onOpen() methods, but they are not required.

For example, here's an implementation of SQLiteOpenHelper that uses some of the commands
shown above:

public class FeedReaderDbHelper extends SQLiteOpenHelper {


// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";

public FeedReaderDbHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

To access your database, instantiate your subclass of SQLiteOpenHelper:

Page 5 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015

FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getContext());

Put information into a database

Insert data into the database by passing a ContentValues object to the insert() method:

// Gets the data repository in write mode


SQLiteDatabase db = dbHelper.getWritableDatabase();

// 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 first argument for insert() is simply the table name.

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.

Read information from a 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();

// Define a projection that specifies which columns from the database


// you will actually use after this query.
String[] projection = {
BaseColumns._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_SUBTITLE

Page 6 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015

};

// Filter results WHERE "title" = 'My Title'


String selection = FeedEntry.COLUMN_NAME_TITLE + " = ?";
String[] selectionArgs = { "My Title" };

// How you want the results sorted in the resulting Cursor


String sortOrder =
FeedEntry.COLUMN_NAME_SUBTITLE + " DESC";

Cursor cursor = db.query(


FeedEntry.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);

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:

List itemIds = new ArrayList<>();


while(cursor.moveToNext()) {
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(FeedEntry._ID));
itemIds.add(itemId);
}
cursor.close();

Page 7 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015

Delete information from a database

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.

// Define 'where' part of query.


String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";
// Specify arguments in placeholder order.
String[] selectionArgs = { "MyTitle" };
// Issue SQL statement.
int deletedRows = db.delete(FeedEntry.TABLE_NAME, selection, selectionArgs);

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();

// New value for one column


String title = "MyNewTitle";
ContentValues values = new ContentValues();
values.put(FeedEntry.COLUMN_NAME_TITLE, title);

// Which row to update, based on the title


String selection = FeedEntry.COLUMN_NAME_TITLE + " LIKE ?";
String[] selectionArgs = { "MyOldTitle" };

int count = db.update(


FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,

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.

Persisting database connection

Since getWritableDatabase() and getReadableDatabase() are expensive to call when the


database is closed, you should leave your database connection open for as long as you possibly
need to access it. Typically, it is optimal to close the database in the onDestroy() of the calling
Activity.

@Override
protected void onDestroy() {
dbHelper.close();
super.onDestroy();
}

Debug your database

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:

Discuss the importance and use cases of SQLite in Android


Application Development.

Minimum of 2 paragraphs and submit through LeOnS Assignment


Activity Module.

For Low Level Technology.

Discuss the importance and use cases of SQLite in Android


Application Development.

Minimum of 2 paragraphs and submit a hand written answer in a


clean paper.

Evaluation Criteria
Points
Criteria
10 5

Grammar is
Grammar Grammatically Correct
understandable enough

Some part of the

Point of discussions are discussions are

Subject Matter in-line with the somewhat off or not

Objective related with the


Objective

Total (20)

VIII. Posttest (Students’ Level Of Technology)


Posttest 1:
For High/Medium level Technology:

Page 10 of 12
Republic of the Philippines
Camarines Sur Polytechnic Colleges
Nabua, Camarines Sur
ISO 9001:2015

Please log in to your CSPC LeOnS and Answer Quiz 2.7.


For Low Level Technology.
This sheet shall be submitted on the next Drop-off/Pick Up schedule, write your name and
section on the space provided.

NAME: ________________________________________________ Course Yr. & Sec.: _____________


Encircle the letter of the correct answer.

1. Which API package is needed to use database on Android?


a) database.android.sqlite
b) sqlite.database.android
c) android.sqlite.database
d) android.database.sqlite

2. Which folder does Android store the database in the device’s Internal Storage?
a) public
b) private
c) protected
d) hidden

3. What method is used to read information for a database?


a) query()
b) insert()
c) update()
d) delete()

4. What does the insert() method returns when a new row is created?
a) 0
b) -1
c) 1
d) ID

5. What is the return value of the delete() method?


a) # of Rows Deleted
b) # of Columns Deleted
c) # of Rows Inserted
d) # of Rows Affected

6. What is the return value of the update() method?


a) # of Rows Deleted
b) # of Columns Deleted
c) # of Rows Inserted
d) # of Rows Affected

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

8. The result of an SQLite query is returned as a _____ object.


a) Row
b) Cursor
c) Result
d) Column

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

You might also like