Mobile Application Development: Lecture 14


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Mobile Application Development: Lecture 14

  1. 1. Mobile Application Development Lecture 14 Vladimir Kulyukin Department of Computer Science Utah State University
  2. 2. Outline ● Persisting Structured Data in SQL Databases ● Lite Intro to SQLite ● SQLite Database Inspection ● SQLite by Example
  3. 3. Persisting Structured Data in SQL Databases
  4. 4. SharedPreferences: Pros & Cons ● Pros – Lightweight – Easy to code and debug ● Cons – Slow with large data volumes – Not helpful with highly structured data when key/value pairs are not sufficient – Entire file needs to be read and parsed to access data – Takes up more space because XML is used for storage
  5. 5. SQL Databases: Pros & Cons ● Pros – Flexible with structured data – Scalable: can handle large data volumes – Robust: can handle incremental changes – Responsive: can handle flexible queries ● Cons – Heavyweight: more code & more memory – Maintenance: Harder to code & debug
  6. 6. Database Design ● Standard database design practices apply on Android ● A key design principle is to manage space well: reduction redundancy via database normalization ● The objective of database normalization is to decompose relations with anomalies to produce smaller, well structured relations ● Database design is more of an art than a science: takes experience & practice ● Helpful resource:
  7. 7. Database Fundamentals ● Database is a persistent collection of structured data ● Data are structured because they are organized in tables ● A Table is similar to an Excel spreadsheet except that its columns have standard SQL data types (e.g., TEXT, INTEGER, FLOAT, etc.) ● In terms of Object-Oriented Modeling, each table represents an Class (e.g., Book, Author) and each column represents an Attribute ● Each table row represents an Object of a specific Class
  8. 8. Sample Table 01: Book ID Title Author Translator ISBN Price 1 The Essential Rumi Jalal al-Din Rumi C. Barks, J. Moyne 9780062509581 17.51 2 The Illuminated Rumi Jalal al-Din Rumi C. Barks 9780767900027 25.04 3 A Year with Rumi: Daily Jalal al-Din Rumi C. Barks 9780060845971 14.92 Readings 4 A Year with Hafiz: Daily Hafiz D. Ladinsky 9780143117544 12.24 Contemplations 5 The Gift Hafiz D. Ladinsky 9780140195811 12.24 - Book is a Class - ID, Title, Author, Translator, ISBN, Price are Attributes - Rows 1 – 5 are Book Objects
  9. 9. Sample Table 02: Author ID Name BirthYear DeathYear Country 1 Jalal al-Din Rumi 1207 1273 Persia 2 Hafiz 1325 1389 Persia - Author is a Class - ID, Name, BirthYear, DeathYear, Country, are Attributes - Row 1 is an Author Object - Row 2 is an Author Object
  10. 10. The ID Column ● In many tables, the ID column represents the primary key ● The primary key is the key that uniquely identifies the row entry object ● When design an SQL table, it is typical (not necessary!) to specify the primary key as an integer and mark it as auto-incrementable: this way every inserted row is guaranteed to have a unique primary key
  11. 11. Lite Intro to SQLite
  12. 12. SQLite on Android ● Android offers SQLite relational database library ● Every application can create its own database over which it has complete control ● SQLite databases should be considered only when you need to manage complex, structured data ● Databases are stored in /data/data/<package_name>/databases/ ● Example: /data/data/org.vkedco.mobappdev.simple_db_app_01/databases/
  13. 13. What is SQLite? ● SQLite is a relational database management system (RDBMS) ● SQLite is a compact C library that is part of the Android software stack ● Since it is a library, it can be integrated into each application without external dependencies: this simplifies transactions and synchronizations, minimizes latency
  14. 14. Command Line DB Access ● Suppose that you have created a database (we will learn how to do this later) and want to interact with it 1. Open a command line 2. CD into platform-tools directory 3. Connect to the device (e.g., adb -e shell) 4. CD into the database directory: cd /data/data/<package>databases> 5. Execute: sqlite3 <database_name> (e.g. sqlite3 book_info.db) Now you can interct with book_info.db via sqlite shell 6. Execute .quit to quit
  15. 15. Discovering TABLE Schemas ● A Table Schema is the specification of column values and types for a given table ● To access table schemas for a DB: 1. Get in to the sqlite shell 2. Execute: .schema You should see something like: CREATE TABLE android_metadata (locale TEXT); CREATE TABLE author (ID integer primary key autoincrement, Name text not null, ...); CREATE TABLE book (ID integer primary key autoincrement, Title text not null, ...);
  16. 16. SQLite by Example
  17. 17. Displaying Tables ● To display the entire table: SELECT * FROM <TABLE>; Examples: – SELECT * FROM BOOK; – SELECT * FROM AUTHOR;
  18. 18. Project Columns ● To project the values of specific columns for each record: SELECT COLUM_NAME_1, …, COLUMN_NAME_n FROM <TABLE>; Examples: – SELECT Author, Title FROM BOOK; – SELECT Title, Author FROM BOOK;
  19. 19. Record Retrieval ● To retrieve records with specific values, use the WHERE clause: SELECT COLUM_NAME_1, …, COLUMN_NAME_n FROM <TABLE> WHERE <COL_VAL_SPEC>; Examples: – SELECT Title FROM BOOK WHERE ID = 3; – SELECT Title, Author FROM BOOK WHERE ID < 3;
  20. 20. Record Ordering ● To order records by attributes, use the ORDER BY clause: SELECT COLUM_NAME_1, …, COLUMN_NAME_n FROM <TABLE> ORDER BY COL_NAME Asc/Desc; Examples: – SELECT Title, Author FROM BOOK ORDER BY Price Asc; – SELECT Title, Author FROM BOOK ORDER BY Price Desc;
  21. 21. References ● ● ●
  22. 22. Feedback Bugs to vladimir dot kulyukin at gmail dot com