Professional Documents
Culture Documents
SAD - Lecture11 - Sqlite DB
SAD - Lecture11 - Sqlite DB
Application
Development
+
SQLiteDB
● Contract
● SQLiteDBHelper
● Cursor
2
+ SQLite
■ SQLite DB - Data organized as tables
WeatherDBHelper
■ Retrieve
■ Update
■ Add
■ Remove
+
What is a Contract?
Why do we need to create one?
Defines what DB design looks like! Contract is what two sides agree on!
+ Contract Class
■ A contract class
■ This lets you change a column name in one place and have
it propagate throughout your code.
+ Organizing a Contract Class
■ 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.
WeatherDBHelper
JUNE 24 CLOUDY 24 13
JUNE 25 SUNNY 26 12
+ ContactsContracts-ContactColumns
■ Display_Name
■ Has_Phone_Number
■ Photo_ID
■ Photo_File_ID
■ Photo_URI
■ Contact_Last_Updated_TimeStamp
+ Sample Contract
■ package com.fall2019.guestlistdemo;
import android.provider.BaseColumns;
■ Read More:
https://developer.android.com/training/basics/data- storage/dat
abases.html
+
WeatherDBHelper
https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper
+ Implementing a DB Helper
■ public class WaitlistDbHelper extends SQLiteOpenHelper {
// The database name
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
// For now simply drop the table and create a new one. This means if you change the
// DATABASE_VERSION the table will be dropped.
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + WaitlistEntry.TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
+ Implementing a DB Helper (Another Example)
■ Demo: WaitListApp
23
+What should the Contract
Look Like?
+ DB Schema
+ Contract
public class WaitlistContract {
// If you change the database schema, you must increment the database version
private static final int DATABASE_VERSION = 1;
// Constructor
public WaitlistDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
The onCreate method is only called if there is no database.
+ onCreate!
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_WAITLIST_TABLE);
}
https://developer.android.com/reference/android/content/ContentValues
+ Reading/Writing/Updating Database
■ ContentValues cv = new ContentValues();
cv.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME,
name);
cv.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE,
partySize);
+ Add a new Guest!
/**
* Adds a new guest to the mDb including the party count and the current timestamp
*
* @param name Guest's name
* @param partySize Number in party
* @return id of new record added
*/
private long addNewGuest(String name, int partySize) {
ContentValues cv = new ContentValues();
cv.put(WaitlistContract.WaitlistEntry.COLUMN_GUEST_NAME, name);
cv.put(WaitlistContract.WaitlistEntry.COLUMN_PARTY_SIZE, partySize);
return mDb.insert(WaitlistContract.WaitlistEntry.TABLE_NAME, null, cv);
}
https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase
+ Insert
+ Reading/Writing/Updating Database
■ Get reference to writable database
https://developer.android.com/reference/android/content/ContentValues
+ The Cursor
+ Reading Information from a Database
+
https://developer.android.com/reference/android/database/sqlite/SQLiteQueryBu ilder
+
+
+ Cursor movement
+ What will this method
return?
private Cursor guesswhatthisdoes() {
return
mDb.query( WaitlistContract.WaitlistEntry.TA
BLE_NAME, null,
null,
null,
null,
null,
WaitlistC
ontr
act.
Wait
listE
ntry.
COL
UM
N_T
IME
STA
MP
+ Get All Guests!
private Cursor getAllGuests() {
return
mDb.query( WaitlistContract.WaitlistEntry.TA
BLE_NAME, null,
null,
null,
null,
null,
WaitlistC
ontr
act.
Wait
listE
ntry.
COL
UM
N_T
IME
STA
MP
+ Select Query
■ Cursor ncursor= (Cursor) mDb.query(
WaitListContract.WaitlistEntry.TABLE_NAME,
new String[]{WaitListContract.WaitlistEntry.COLUMN_GUEST_NAME},
WaitListContract.WaitlistEntry._ID + "=" + guest_id,
null,
null,
null,
Wait
List
Con
tract
■ + {
public void show_guest(View view)
http://sqlitebrowser.org