SQLite in Android Landon Cox March 2, 2017.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas.
Query Methods (SQL). What is SQL A programming language for databases. SQL (structured Query Language) It allows you add, edit, delete and run queries.
 data/data-storage.html#pref data/data-storage.html#pref 
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Database Rung-Hung Gau Department of Computer Science and Engineering
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
SQLite (part deux) 1 CS440. Traditional Model View Controller (MVC) CS440 2.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Android - SQLite Database 12/10/2015. Introduction SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Working with MySQL A290/A590, Fall /07/2014.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
CHAPTER 9 File Storage Shared Preferences SQLite.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Introduction to Database Programming with Python Gary Stewart
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
CS371m - Mobile Computing Persistence - SQLite. 2 In case you have not taken 347: Data Management or worked with databases as part of a job, internship,
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Dept. of Computer & Information Sciences
N5 Databases Notes Information Systems Design & Development: Structures and links.
Making content providers
Android Application -Architecture.
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Basic Activities and Intents
Mobile Applications (Android Programming)
Android Application SQLite 1.
Reactive Android Development
Firebase Realtime Database
Android Database using SQLite
Mobile Application Development Chapter 5 [Persistent Data in Android]
Activities and Intents
JDBC.
The Android Activity Lifecycle
Lecture 8: Database Topics: Basic SQLite Operations.
SQL – Application Persistence Design Patterns
Android training in Chandigarh. What is ADB ADB stands for Android Debug Bridge. It is a command line tool that is used to communicate with the emulator.
Mobile Computing With Android ACST Android Database Storage Part 2
CS1222 Using Relational Databases and SQL
Activity Lifecycle Fall 2012 CS2302: Programming Principles.
CMPE419 Mobile Application Development
CS371m - Mobile Computing Persistence - SQLite.
Structured Query Language
Mobile Computing With Android ACST 4550 Android Database Storage
Activities and Intents
Computer Science Projects Database Theory / Prototypes
Getting to First Base: Introduction to Database Concepts
Activity Lifecycle.
Android Developer Fundamentals V2
Getting to First Base: Introduction to Database Concepts
Chapter 11 Managing Databases with SQL Server 2000
Getting to First Base: Introduction to Database Concepts
ListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListAdapter is used to.
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
SQL – Application Persistence Design Patterns
Presentation transcript:

SQLite in Android Landon Cox March 2, 2017

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

Launch Process killed Running Shut down onCreate() onStart() onRestart() onResume() Process killed Running onPause() onStop() onDestroy() Shut down

Saving quiz state Last time: we remembered which quiz we were taking 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

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!

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

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

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 Duke Basketball 1488460945

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); $> ~/Library/Android/sdk/platform-tools/adb shell $> su $> sqlite3 /data/data/edu.duke.compsci290.quizmaster/databases/

Keywords: create, table Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Keywords: create, table 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 Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Table name: quizprogress CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);

Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column name: _ID CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);

Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column type: INT 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 Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Unique to each row: PRIMARY KEY 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 Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column name: quiz_title CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);

Create the database SQLiteOpenHelper class Gives references to SQLiteDatabase instance constructor: super call to create db onCreate: create SQL command to create tables Column type: TEXT CREATE TABLE quizprogress (_ID INT PRIMARY KEY, quiz_title TEXT, num_correct INT, num_wrong INT, last_question INT, finished_quiz INT, timestamp INT);

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

Put information in the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method Keywords: INSERT, INTO, VALUES INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, 1488460945) ;

Put information in the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can use insert method List of columns to update INSERT INTO quizprogress (quiz_title, num_correct, num_wrong, last_question, finished_quiz, timestamp) VALUES (“Duke Basketball”, 0, 0, 0, 0, 1488460945) ;

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

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;

Keywords: SELECT, FROM, WHERE, LIKE, ORDER BY, DESC 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; Keywords: SELECT, FROM, WHERE, LIKE, ORDER BY, DESC

Columns in result: _ID, timestamp 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

Table to query: quizprogress 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

Filter rows using quiz_title 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

Return results in descending order using timestamp 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

Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = 1488460945 WHERE quiz_title LIKE “Duke Basketball”;

Keywords: UPDATE, SET, WHERE, LIKE Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = 1488460945 WHERE quiz_title LIKE “Duke Basketball”; Keywords: UPDATE, SET, WHERE, LIKE

Table to use: quizprogress Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method Table to use: quizprogress UPDATE quizprogress SET timestamp = 1488460945 WHERE quiz_title LIKE “Duke Basketball”;

New column value: timestamp Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method New column value: timestamp UPDATE quizprogress SET timestamp = 1488460945 WHERE quiz_title LIKE “Duke Basketball”;

Specify which rows to update Update the database SQLiteDatabase Can submit raw SQL queries w/ execSQL Can also use update method UPDATE quizprogress SET timestamp = 1488460945 WHERE quiz_title LIKE “Duke Basketball”; Specify which rows to update