Download presentation
Presentation is loading. Please wait.
1
SQLite in Android Landon Cox March 2, 2017
2
What is storage? Storage is persistent Stored state retains its value across launches, reboots Primary interface to storage: files Files can be opened, read, written to, and closed Databases are often implemented on top of files Databases provide a higher-level storage interface (e.g., SQL) Java provides a wealth of ways to interact with storage/files Your code isn’t going to be very useful without storage
3
Launch Process killed Running Shut down onCreate() onStart()
onRestart() onResume() Process killed Running onPause() onStop() onDestroy() Shut down
4
Saving quiz state Last time: we remembered which quiz we were taking
Apps will need to write to storage too Users may expect to pick up where they left off if the app exits What quiz state should you save in case the app exits? Several ways to do this SharedPreferences for simple key-value maps SQLite for more complex kinds of state SharedPreferences Get a reference to SharedPreferences through Context To write: edit, put*, commit To read: get* Last time: we remembered which quiz we were taking
5
Saving quiz state What else might we want to save?
Which quizzes we’ve taken The user’s progress on each quiz The user’s final score on completed quiz How long the user took to complete the quiz Using a key-value store for all of this will become unwieldy Instead: use a relational database like SQLite!
6
Relational databases Data is organized into tables
Tables have named, typed columns Data is stored as rows in a table Can place constraints on columns (e.g., uniqueness) Structure + constraints define the schema Read/write the data base with SQL Structured Query Language (SQL) SQL is declarative It describes what result you want, not how to compute it Example databases: mysql, postgresql, sqlite
7
SQLite SQLite is the primary database for Android apps
Classes for managing your app’s SQLite database Contract class w/ inner BaseColumns class DbHelper class that extends SQLiteOpenHelper Cursor for iterating through answers to queries
8
Define the contract/schema
Contract class Place to put all constants related to your database BaseColumns inner class Table names Column names One BaseColumns class for each table in the db _id quiz_title num_correct num_wrong last_question finished_quiz timestamp Duke Basketball
9
Create the database SQLiteOpenHelper class
Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT); $> ~/Library/Android/sdk/platform-tools/adb shell $> su $> sqlite3 /data/data/edu.duke.compsci290.quizmaster/databases/
10
Keywords: create, table
Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Keywords: create, table CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
11
Table name: quizprogress
Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Table name: quizprogress CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
12
Create the database SQLiteOpenHelper class
Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column name: _ID CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
13
Create the database SQLiteOpenHelper class
Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column type: INT CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
14
Unique to each row: PRIMARY KEY
Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Unique to each row: PRIMARY KEY CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
15
Column name: quiz_title
Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column name: quiz_title CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
16
Create the database SQLiteOpenHelper class
Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column type: TEXT CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);
17
Put information in the database
SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, ) ;
18
Put information in the database
SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method Keywords: INSERT, INTO, VALUES INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, ) ;
19
Put information in the database
SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method List of columns to update INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, ) ;
20
Put information in the database
SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, ) ; Values for each column in new row
21
Read from the database SQLiteDatabase
Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC;
22
Keywords: SELECT, FROM, WHERE, LIKE, ORDER BY, DESC
Read from the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC; Keywords: SELECT, FROM, WHERE, LIKE, ORDER BY, DESC
23
Columns in result: _ID, timestamp
Read from the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC; Columns in result: _ID, timestamp
24
Table to query: quizprogress
Read from the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC; Table to query: quizprogress
25
Filter rows using quiz_title
Read from the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC; Filter rows using quiz_title
26
Return results in descending order using timestamp
Read from the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use query method SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball” ORDER BY timestamp DESC; Return results in descending order using timestamp
27
Update the database SQLiteDatabase
Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = WHERE quiz_title LIKE “Duke Basketball”;
28
Keywords: UPDATE, SET, WHERE, LIKE
Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = WHERE quiz_title LIKE “Duke Basketball”; Keywords: UPDATE, SET, WHERE, LIKE
29
Table to use: quizprogress
Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method Table to use: quizprogress UPDATE quizprogress SET timestamp = WHERE quiz_title LIKE “Duke Basketball”;
30
New column value: timestamp
Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method New column value: timestamp UPDATE quizprogress SET timestamp = WHERE quiz_title LIKE “Duke Basketball”;
31
Specify which rows to update
Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = WHERE quiz_title LIKE “Duke Basketball”; Specify which rows to update
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.