12
Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald Waterford Institute of Technology, Mobile Application Development SQLite database 1/12

Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

  • Upload
    others

  • View
    13

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

Mobile Application DevelopmentSQLite database

Waterford Institute of Technology

October 11, 2016

John Fitzgerald

Waterford Institute of Technology, Mobile Application Development SQLite database 1/12

Page 2: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteRelational Database Management System (DBMS)

• Tenth most widely used (DB-Engines)• Users include

• Adobe• Airbus• Apple• Bosch (car multimedia systems)

Waterford Institute of Technology, Mobile Application Development SQLite database 2/12

Page 3: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteSuitable Uses

• Mobile apps• Embedded devices• Internet of Things (IoT)• Websites• Data analysis (using sqlite3 commandline shell)• Demos & testing in enterprise environments• Education & training

Waterford Institute of Technology, Mobile Application Development SQLite database 3/12

Page 4: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteCreate

public void addResidence(Residence residence){

SQLiteDatabase db = this.getWritableDatabase();ContentValues values = new ContentValues();values.put(PRIMARY_KEY, residence.id.toString());values.put(GEOLOCATION, residence.geolocation);// Insert recorddb.insert(TABLE_RESIDENCES, null, values);db.close();

}

Waterford Institute of Technology, Mobile Application Development SQLite database 4/12

Page 5: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteRead

public Residence selectResidence(UUID resId) {Residence residence;SQLiteDatabase db = this.getReadableDatabase();Cursor cursor = null;try {

residence = new Residence();...

} finally {cursor.close();

}return residence;

}

Waterford Institute of Technology, Mobile Application Development SQLite database 5/12

Page 6: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteDelete

public void deleteResidence(Residence residence) {SQLiteDatabase db = this.getWritableDatabase();try {

db.delete("tableResidences", "id" + "=?", newString[]{residence.id.toString() + ""});

} catch (Exception e) {Log.d(TAG, "delete residence failure: " + e.getMessage

()); }}

Waterford Institute of Technology, Mobile Application Development SQLite database 6/12

Page 7: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteRead (all records)

public List<Residence> selectAllResidences() {

List<Residence> residences= new ArrayList<Residence>();

String query ="SELECT ∗ FROM " + "tableResidences";......

return residences;}

Waterford Institute of Technology, Mobile Application Development SQLite database 7/12

Page 8: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteDelete (all records)

public void deleteAllResidences() {SQLiteDatabase db = this.getWritableDatabase();try {

db.execSQL("delete from tableResidences");} catch (Exception e) {

Log.d(TAG, "delete residences failure: " + e.getMessage());

}}

Waterford Institute of Technology, Mobile Application Development SQLite database 8/12

Page 9: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

SQLiteUpdate (single record - preserve primary key)

public void updateResidence(Residence residence) {SQLiteDatabase db = this.getWritableDatabase();try {

ContentValues values = new ContentValues();values.put(GEOLOCATION, residence.geolocation);...values.put(PHOTO, residence.photo);db.update("tableResidences", ...);

} catch (Exception e) {Log.d(TAG, "update residences failure: " + e.

getMessage());}

}

Waterford Institute of Technology, Mobile Application Development SQLite database 9/12

Page 10: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

Android Debug Bridge (ADB)Examine tables using commandline tools

ADB Shell Commands

// Launch the adb shelladb shell

// Change into data foldercd data/data/

// List the databasesls databases

// Change into database foldercd sqlite.myrentsqlite

// Run sqlite3 to access tablessqlite3 residences.db

Waterford Institute of Technology, Mobile Application Development SQLite database 10/12

Page 11: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

Referenced Material

1. SQLite Documentationhttps://www.sqlite.org/docs.html

[Accessed 2016-10-10]

Waterford Institute of Technology, Mobile Application Development SQLite database 11/12

Page 12: Mobile Application Development SQLite database · 11/10/2016  · Mobile Application Development SQLite database Waterford Institute of Technology October 11, 2016 John Fitzgerald

Waterford Institute of Technology, Mobile Application Development SQLite database 12/12