Android™ Database Best Practices

Book description

Battle-Tested Strategies for Storing, Managing, and Sharing Android Data

“Android™ Database Best Practices goes well beyond API documentation to offer strategic advice about how to handle data in an Android application and the tools needed to develop productively. This arms the developer with a trove of solutions to nearly any problem an application may face involving data. Mastering the concepts in this book are therefore essential for any developer who wants to create professional Android applications.” –Greg Milette, Android developer, Gradison Technologies, Inc.

This is the first guide to focus on one of the most critical aspects of Android development: how to efficiently store, retrieve, manage, and share information from your app’s internal database. Through real-world code examples, which you can use in your own apps, you’ll learn how to take full advantage of SQLite and the database-related classes on Android.

A part of Addison-Wesley’s Android™ Deep Dive series for experienced Android developers, Android Database Best Practices draws on Adam Stroud’s extensive experience leading cutting-edge app projects.

Stroud reviews the core database theory and SQL techniques you need to efficiently build, manipulate, and read SQLite databases. He explores SQLite in detail, illuminates Android’s APIs for database interaction, and shares modern best practices for working with databases in the Android environment.

Through a complete case study, you’ll learn how to design your data access layer to simplify all facets of data management and avoid unwanted technical debt. You’ll also find detailed solutions for common challenges in building data-enabled Android apps, including issues associated with threading, remote data access, and showing data to users. Extensive, up-to-date sample code is available for download at github.com/android-database-best-practices/device-database.

You will

  • Discover how SQLite database differs from other relational databases

  • Use SQL DDL to add structure to a database, and use DML to manipulate data

  • Define and work with SQLite data types

  • Persist highly structured data for fast, efficient access

  • Master Android classes for create, read, update, and delete (CRUD) operations and database queries

  • Share data within or between apps via content providers

  • Master efficient UI strategies for displaying data, while accounting for threading issues

  • Use Android’s Intents API to pass data between activities when starting a new activity or service

  • Achieve two-way communication between apps and remote web APIs

  • Manage the complexities of app-to-server communication, and avoid common problems

  • Use Android’s new Data Binding API to write less code and improve performance

  • Table of contents

    1. About This eBook
    2. Title Page
    3. Copyright Page
    4. About the Android Deep Dive Series
      1. About the Series Editor
    5. Dedication Page
    6. Contents in Brief
    7. Contents
    8. Preface
      1. Who Should Read This Book
      2. How This Book Is Organized
      3. Example Code
      4. Conventions Used in This Book
    9. Acknowledgments
    10. About the Author
    11. 1. Relational Databases
      1. History of Databases
        1. Hierarchical Model
        2. Network Model
        3. The Introduction of the Relational Model
      2. The Relational Model
        1. Relation
          1. Attribute
          2. Tuples
          3. Intension/Extension
          4. Schema
        2. Properties of a Relation
        3. Relationships
          1. Referential Integrity
      3. Relational Languages
        1. Relational Algebra
          1. Union (A ∪ B)
          2. Intersection (A ∩ B)
          3. Difference (A - B)
          4. Cartesian Product (A × B)
          5. Selection (σpredicate (σ))
          6. Projection (Πa1, a2,…,an(A))
          7. Joins
        2. Relational Calculus
          1. Tuple Relational Calculus
          2. Domain Relational Calculus
      4. Database Languages
        1. ALPHA
        2. QUEL
        3. SEQUEL
      5. Summary
    12. 2. An Introduction to SQL
      1. Data Definition Language
        1. Tables
          1. CREATE TABLE
          2. ALTER TABLE
          3. DROP TABLE
        2. Indexes
          1. CREATE INDEX
          2. DROP INDEX
        3. Views
          1. CREATE VIEW
          2. DROP VIEW
        4. Triggers
          1. CREATE TRIGGER
          2. DROP TRIGGER
      2. Data Manipulation Language
        1. INSERT
          1. VALUES
          2. SELECT
          3. DEFAULT
        2. UPDATE
        3. DELETE
      3. Queries
        1. ORDER BY
        2. Joins
      4. Summary
    13. 3. An Introduction to SQLite
      1. SQLite Characteristics
      2. SQLite Features
        1. Foreign Key Support
        2. Full Text Search
        3. Atomic Transactions
          1. Journal Mode
          2. Write-Ahead-Log (WAL) Mode
        4. Multithread Support
      3. What SQLite Does Not Support
        1. Limited JOIN Support
        2. Read-Only Views
        3. Limited ALTER TABLE Support
      4. SQLite Data Types
        1. Storage Classes
        2. Type Affinity
      5. Summary
    14. 4. SQLite in Android
      1. Data Persistence in Phones
      2. Android Database API
        1. SQLiteOpenHelper
          1. SQLiteOpenHelper Constructors
          2. SQLiteOpenHelper.onCreate()
          3. SQLiteOpenHelper.onUpgrade()
          4. SQLiteOpenHelper.onConfigure()
          5. SQLiteOpenHelper.onDowngrade()
          6. Putting It All Together
        2. SQLiteDatabase
      3. Strategies for Upgrading Databases
        1. Rebuilding the Database
        2. Manipulating the Database
        3. Copying and Dropping Tables
      4. Database Access and the Main Thread
      5. Exploring Databases in Android
        1. Accessing a Database with adb
          1. Introduction to adb
          2. Permissions and the Android File System
          3. Finding a Database Location with adb
          4. Connecting to a Database with sqlite3
          5. A Shorthand Approach to adb and sqlite3
        2. Using Third-Party Tools to Access Android Databases
          1. Accessing a Database with Stetho
      6. Summary
    15. 5. Working with Databases in Android
      1. Manipulating Data in Android
        1. Inserting Rows into a Table
        2. Updating Rows in a Table
        3. Replacing Rows in a Table
        4. Deleting Rows from a Table
      2. Transactions
        1. Using a Transaction
        2. Transactions and Performance
      3. Running Queries
        1. Query Convenience Methods
        2. Raw Query Methods
      4. Cursors
        1. Reading Cursor Data
        2. Managing the Cursor
      5. CursorLoader
        1. Creating a CursorLoader
        2. Starting a CursorLoader
        3. Restarting a CursorLoader
      6. Summary
    16. 6. Content Providers
      1. REST-Like APIs in Android
      2. Content URIs
      3. Exposing Data with a Content Provider
        1. Implementing a Content Provider
          1. onCreate()
          2. insert()
          3. delete()
          4. getType()
          5. query()
          6. update()
          7. bulkInsert() and applyBatch()
        2. Content Resolver
      4. Exposing a Remote Content Provider to External Apps
        1. Provider-Level Permission
        2. Individual Read/Write Permissions
        3. URI Path Permissions
        4. Content Provider Permissions
      5. Content Provider Contract
      6. Allowing Access from an External App
      7. Implementing a Content Provider
        1. Extending android.content.ContentProvider
        2. insert()
        3. delete()
        4. update()
        5. query()
        6. getType()
      8. When Should a Content Provider Be Used?
        1. Content Provider Weaknesses
          1. The Need for Extra Code
          2. Use of URIs and Cursors over Objects
          3. No Convenient Place to Close the Database
        2. Content Provider Strengths
          1. Abstraction Layer for Structured Data
          2. Well Supported by Other Android Components
          3. Handles Interprocess Communication
      9. Summary
    17. 7. Databases and the UI
      1. Getting Data from the Database to the UI
        1. Using a Cursor Loader to Handle Threading
        2. Binding Cursor Data to a UI
          1. ListView
          2. RecyclerView
      2. Cursors as Observers
        1. registerContentObserver(ContentObserver)
        2. registerDataSetObserver(DataSetObserver)
        3. unregisterContentObserver(ContentObserver)
        4. unregisterDataSetObserver(DataSetObserver)
        5. setNotificationUri(ContentResolver, Uri uri)
      3. Accessing a Content Provider from an Activity
        1. Activity Layout
        2. Activity Class Definition
        3. Creating the Cursor Loader
        4. Handling Returned Data
        5. Reacting to Changes in Data
      4. Summary
    18. 8. Sharing Data with Intents
      1. Sending Intents
        1. Explicit Intents
        2. Implicit Intents
        3. Starting a Target Activity
      2. Receiving Implicit Intents
      3. Building an Intent
        1. Actions
          1. Intent.ACTION_SEND
          2. Intent.ACTION_SEND_MULTIPLE
        2. Extras
          1. EXTRA_TEXT
          2. EXTRA_STREAM
        3. Extra Data Types
          1. Implementing Parcelable
          2. Writing a Parcel
          3. CREATOR
          4. Reading a Parcel
        4. What Not to Add to an Intent
      4. ShareActionProvider
        1. Share Action Menu
      5. Summary
    19. 9. Communicating with Web APIs
      1. REST and Web Services
        1. REST Overview
        2. REST-like Web API Structure
      2. Accessing Remote Web APIs
        1. Accessing Web Services with Standard Android APIs
          1. Communicating with the Web Service
          2. Working with JSON
        2. Accessing Web Services with Retrofit
          1. Adding Retrofit to an Android Project
          2. Using Retrofit
        3. Accessing Web Services with Volley
          1. Adding Volley to an Android Project
          2. Using Volley
      3. Persisting Data to Enhance User Experience
        1. Data Transfer and Battery Consumption
        2. Data Transfer and User Experience
        3. Storing Web Service Response Data
      4. Android SyncAdapter Framework
        1. AccountAuthenticator
        2. SyncAdapter
      5. Manually Synchronizing Remote Data
        1. A Short Introduction to RxJava
        2. Adding RxJava Support to Retrofit
        3. Using RxJava to Perform the Sync
      6. Summary
    20. 10. Data Binding
      1. Adding Data Binding to an Android Project
      2. Data Binding Layouts
        1. Binding an Activity to a Layout
        2. Using a Binding to Update a View
        3. Reacting to Data Changes
      3. Using Data Binding to Replace Boilerplate Code
      4. Data Binding Expression Language
      5. Summary
    21. Index
    22. Code Snippets

    Product information

    • Title: Android™ Database Best Practices
    • Author(s): Adam Stroud
    • Release date: July 2016
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780134438092