Unit 4: SQLite database connectivity
Mobile Application Development notes · PTU syllabus (BSIT601)
On this page
Unit summary
SQLite gives every Android app a full relational database. This unit covers SQLiteOpenHelper and database creation, opening and closing a database, cursors, inserts, updates and deletes, SQLite data types, ContentValues, content providers and querying providers.
After this unit you can
- Create a database with SQLiteOpenHelper
- Insert, update, delete and query records with ContentValues and cursors
- Explain SQLite data types
- Use and create content providers
PTU syllabus topics
- SQLiteOpenHelper and database creation
- opening/closing a database
- cursors and their types
- inserts/updates/deletes
- SQLite data types
- content values
- content providers and query providers
- SQLiteOpenHelper
- Creates and upgrades the database
- getWritableDatabase()
- Opens it for reading and writing
- ContentValues
- Holds column-value pairs for insert or update
- Cursor
- Iterates over query results
- ContentProvider
- Shares data with other apps
Topic 1
SQLiteOpenHelper and database creation
javapublic class DBHelper extends SQLiteOpenHelper {
static final String DB = "college.db";
static final int VERSION = 1;
public DBHelper(Context c) { super(c, DB, null, VERSION); }
@Override public void onCreate(SQLiteDatabase db) { // first use
db.execSQL("CREATE TABLE student (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT NOT NULL, course TEXT, marks INTEGER)");
}
@Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { // version change
db.execSQL("DROP TABLE IF EXISTS student");
onCreate(db);
}
}- onCreate runs once when the database file is first created; onUpgrade runs when VERSION increases — migrate data rather than dropping tables in real apps.
Topic 2
Opening and closing a database
- getWritableDatabase() opens for reading and writing (creating or upgrading if needed); getReadableDatabase() opens for reading (falls back to read-only if the disk is full); close() releases the connection, usually when the helper is no longer needed.
Topic 3
ContentValues and inserts
javaSQLiteDatabase db = new DBHelper(this).getWritableDatabase();
ContentValues cv = new ContentValues(); // key–value set of column values
cv.put("name", "Aman");
cv.put("course", "B.Sc IT");
cv.put("marks", 82);
long id = db.insert("student", null, cv); // returns row id, or −1 on errorTopic 4
Updates and deletes
javaContentValues upd = new ContentValues();
upd.put("marks", 88);
int rows = db.update("student", upd, "_id = ?", new String[]{String.valueOf(id)});
int deleted = db.delete("student", "marks < ?", new String[]{"40"});Exam tip
Use ? placeholders with selection arguments instead of joining strings into SQL — it prevents SQL injection and quoting errors.
Topic 5
Cursors and their types
javaCursor c = db.query("student", new String[]{"_id", "name", "marks"},
"course = ?", new String[]{"B.Sc IT"}, null, null, "marks DESC");
// or: db.rawQuery("SELECT name, marks FROM student WHERE marks >= ?", new String[]{"60"});
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndexOrThrow("name"));
int marks = c.getInt(c.getColumnIndexOrThrow("marks"));
}
c.close();- SQLiteCursor
- Default cursor returned by query on a database
- MatrixCursor
- In-memory cursor built from arrays — useful for content providers
- MergeCursor
- Joins several cursors one after another
- CursorWrapper
- Wraps a cursor to add behaviour
- Movement methods
- moveToFirst, moveToNext, moveToPosition, isAfterLast
- Data methods
- getCount, getColumnIndex, getString, getInt, close
Topic 6
SQLite data types
NULL
Missing value
—
INTEGER
Signed whole numbers up to 8 bytes
Also used for booleans (0, 1) and dates as Unix time
REAL
8-byte floating point
Amounts and measurements
TEXT
Strings (UTF-8)
Dates as ISO text "2026-10-08"
BLOB
Binary data stored as given
Small images; larger files better stored as files
- SQLite uses dynamic typing with type affinity: a column's declared type suggests, but does not force, the storage class.
Topic 7
Content providers and querying providers
- Content provider: a component that offers an app's data to other apps through a standard interface, addressed by a content URI — content://authority/path/id.
- 1Declare and request READ_CONTACTS permission
- 2Get a ContentResolver with getContentResolver()
- 3Call query() with ContactsContract URI, projection, selection and sort order
- 4Iterate the returned Cursor
- 5Close the cursor
javaCursor c = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");- Creating a provider: extend ContentProvider and implement onCreate, query, insert, update, delete and getType; declare it in the manifest with an authority; use a UriMatcher to map URIs to tables.
- Built-in providers: Contacts, Calendar, MediaStore (images, audio, video), Settings, call log.
Key terms
- SQLiteOpenHelper
- Class managing database creation and version upgrades
- ContentValues
- Set of column–value pairs for inserts and updates
- Cursor
- Object for reading rows returned by a query
- Type affinity
- Recommended storage class for a SQLite column
- Content provider
- Component sharing app data through content URIs
Quick revision
- SQLiteOpenHelper: constructor, onCreate, onUpgrade, versions.
- getWritableDatabase, getReadableDatabase, close.
- insert with ContentValues; update and delete with selection arguments.
- query and rawQuery; cursor movement; cursor types.
- NULL, INTEGER, REAL, TEXT, BLOB; content URIs; ContentResolver; creating providers.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.When is onUpgrade() called?
- Q2.Distinguish getWritableDatabase() and getReadableDatabase().
- Q3.What is ContentValues?
- Q4.Name the storage classes of SQLite.
- Q5.What does moveToNext() return?
- Q6.What is a content URI?
Long-answer questions
- Q1.Explain SQLiteOpenHelper with a program.
- Q2.Write code to insert, update, delete and display records using SQLite.
- Q3.Explain cursors and SQLite data types.
- Q4.Explain content providers and how to query them.
Stuck on this unit?
Message SBS on WhatsApp for help with Mobile Application Development, or to ask about studying B.Sc IT at Synetic.
