sqlite in android - duke university

31
SQLite in Android Landon Cox March 2, 2017

Upload: others

Post on 29-Nov-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQLite in Android - Duke University

SQLite in Android

Landon Cox March 2, 2017

Page 2: SQLite in Android - Duke University

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

Page 3: SQLite in Android - Duke University

Launch

Shut down

Running

onCreate()

onStart()

onResume()

onPause()

onStop()

onDestroy()

onRestart()

Process killed

Page 4: SQLite in Android - Duke University

Saving quiz state •  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

Page 5: SQLite in Android - Duke University

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!

Page 6: SQLite in Android - Duke University

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

Page 7: SQLite in Android - Duke University

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

https://developer.android.com/training/basics/data-storage/databases.html

Page 8: SQLite in Android - Duke University

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

0 Duke Basketball 0 0 0 0 1488460945

Page 9: SQLite in Android - Duke University

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

Page 10: SQLite in Android - Duke University

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

Keywords: create, table

Page 11: SQLite in Android - Duke University

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

Table name: quizprogress

Page 12: SQLite in Android - Duke University

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

Column name: _ID

Page 13: SQLite in Android - Duke University

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

Column type: INT

Page 14: SQLite in Android - Duke University

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

Unique to each row: PRIMARY KEY

Page 15: SQLite in Android - Duke University

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

Column name: quiz_title

Page 16: SQLite in Android - Duke University

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

Column type: TEXT

Page 17: SQLite in Android - Duke University

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, 1488460945) ;

Page 18: SQLite in Android - Duke University

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, 1488460945) ;

Keywords: INSERT, INTO, VALUES

Page 19: SQLite in Android - Duke University

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, 1488460945) ;

List of columns to update

Page 20: SQLite in Android - Duke University

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, 1488460945) ;

Values for each column in new row

Page 21: SQLite in Android - Duke University

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;

Page 22: SQLite in Android - Duke University

Read from the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use query method

Keywords: SELECT, FROM, WHERE, LIKE, ORDER BY, DESC

SELECT _ID, timestamp FROM quizprogress WHERE quiz_title LIKE “Duke Basketball”ORDER BY timestamp DESC;

Page 23: SQLite in Android - Duke University

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

Page 24: SQLite in Android - Duke University

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

Page 25: SQLite in Android - Duke University

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

Page 26: SQLite in Android - Duke University

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

Page 27: SQLite in Android - Duke University

Update the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use update method

UPDATE quizprogressSET timestamp = 1488460945WHERE quiz_title LIKE “Duke Basketball”;

Page 28: SQLite in Android - Duke University

Update the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use update method

Keywords: UPDATE, SET, WHERE, LIKE

UPDATE quizprogressSET timestamp = 1488460945WHERE quiz_title LIKE “Duke Basketball”;

Page 29: SQLite in Android - Duke University

Update the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use update method

Table to use: quizprogress

UPDATE quizprogressSET timestamp = 1488460945WHERE quiz_title LIKE “Duke Basketball”;

Page 30: SQLite in Android - Duke University

Update the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use update method

New column value: timestamp

UPDATE quizprogressSET timestamp = 1488460945WHERE quiz_title LIKE “Duke Basketball”;

Page 31: SQLite in Android - Duke University

Update the database •  SQLiteDatabase

•  Can submit raw SQL queries w/ execSQL •  Can also use update method

Specify which rows to update

UPDATE quizprogressSET timestamp = 1488460945WHERE quiz_title LIKE “Duke Basketball”;