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,

Slides:



Advertisements
Similar presentations
MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Advertisements

The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
PHP (2) – Functions, Arrays, Databases, and sessions.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Database Updates Made Easy In WebFocus Using SQL And HTML Painter Sept 2011 Lender Processing Services 1.
CS378 - Mobile Computing Persistence - SQLite. Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd.
Data Persistence in Android
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
CSCI 6962: Server-side Design and Programming
Module 3: Table Selection
Session 5: Working with MySQL iNET Academy Open Source Web Development.
FIRST COURSE Access Tutorial 1 Creating a Database.
Databases and LINQ Visual Basic 2010 How to Program 1.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
Concepts of Database Management Seventh Edition
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
PHP meets MySQL.
® Microsoft Access 2010 Tutorial 1 Creating a Database.
CODD’s 12 RULES OF RELATIONAL DATABASE
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
1 Working with MS SQL Server Textbook Chapter 14.
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.
Concepts of Database Management Seventh Edition
Chapter 17 Creating a Database.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
M1G Introduction to Database Development 5. Doing more with queries.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
® Microsoft Office 2013 Access Creating a Database.
Microsoft Access. Microsoft access is a database programs that allows you to store retrieve, analyze and print information. Companies use databases for.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
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.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
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.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
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.
1. 2 The Address Book app provides convenient access to contact information that’s stored in a SQLite database on the device. You can: scroll through.
Making content providers
Visual Basic 2010 How to Program
Mobile Applications (Android Programming)
SQLite in Android Landon Cox March 2, 2017.
Practical Office 2007 Chapter 10
Mobile Applications (Android Programming)
Android Application SQLite 1.
CS371m - Mobile Computing Persistence - SQLite.
Developing a Model-View-Controller Component for Joomla Part 3
Mobile Computing With Android ACST 4550 Android Database Storage
Computer Science Projects Database Theory / Prototypes
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

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, or project:

Databases RDBMS – relational data base management system Relational databases introduced by E. F. Codd in the 1970s – Turing Award Winner Relational Database – data stored in tables – relationships among data stored in tables – data can be accessed and viewed in different ways 3

Example Database Wines 4 Web Database Applications with PHP and MySQL, 2nd Edition, by Hugh E. Williams, David Lane

Relational Data Data in different tables can be related – hence, relational database 5

Keys Each table has a key Column used to uniquely identify each row 6 KEYS

SQL and SQLite Structured Query Language a programming language to manage data in a RDBMS SQLite implements most, but not all of SQL – 7

Aside - Database Admins full time jobs One example: ERCOT = Electric Reliability Council of Texas 8

SQLite and Android Databases created with applications are accessible by name to all classes in application, but no outside applications Creating database: – create subclass of SQLiteOpenHelper and override onCreate() method – execute SQLite command to create tables in database – onUpgrade() method for later versions of app and database already present 9

SQL and Databases SQL is a language used to manipulate and manage information in a relational database management system (RDBMS) SQL Commands: CREATE TABLE - creates a new database table ALTER TABLE - alters a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index 10

SQL Commands SELECT - get data from a database table UPDATE - change data in a database table DELETE - remove data from a database table INSERT INTO - insert new data in a database table 11

ANDROID AND SQLITE 12

Android and SQLite SQLite "baked into" Android. Device will have SQLite and apps can create and use databases. Not necessary to add third party library or jar to your app. Many developers use a third party library to ease the syntax burden of using SQLite directly in their code. 13

Android and SQLite SQLiteDatabase class methods to programmatically interact with SQLite database SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks. database restricted to application – unless create content provider 14

Android and SQLite Build database on the fly in application example (movie ratings) has no built in data to start with possible to create database ahead of time and include in apk move from apk to Android database on first use 15

Creating Database Example: Movie Rating App Stores user ratings Not a complex example Database only has one table – overkill in this scenario Adapted from Deitel Address Book Application 16

Classes 17 MovieRaterActivity Starting Activity Displays List of RatedMovies AddEditRating Add or Edit Rating menu - Add Rating ViewRating Show Rating and Information click on Movie Title menu - Edit Rating menu - Delete Rating Row remove from database DatabaseConnector Interact With Database

MovieRaterActivity ScrollView Queries data base for all names / titles Clicking on Title brings up that rating in ViewRating 18

Menu for MovieRaterActivity Only one menu option / action bar item button to Add Rating Brings up AddEditRating Activity 19

ViewRating Pulls all data from database for row based on name / title Use of a RatingBar ViewRating has its own Action Bar items 20

ViewRating Menu Edit Rating starts AddEditRating activity and populates fields with these values (place in Extras) Delete Rating brings up confirmation Dialog 21 Edit RatingDelete Rating

AddEditRating Add Rating – fields are blank Consider adding a button for date picker instead of typing data Must enter title / name other fields can be blank 22

AddEditRating When title clicked in main Activity, MovieRaterActivity Make changes and click save 23

DatabaseConnector Class Start of class 24

DatabaseConnector Class 25

Creating Database Via an inner class that extends SQLiteOpenHelper Used to create database first time app run on a device can also be used to update database if you update your app and alter the structure of the database 26

Creating Database The money method in DatabaseOpenHelper 27

Creating Database String is a SQLite command ratings is name of table table has seven columns – _id, name, genre, dateSeen, tag1, tag2, rating storage classes for columns: – TEXT, INTEGER, REAL – also NULL and BLOB (Binary Large OBject) _id is used as primary key for rows 28

Updating Database Quite likely you change the set up of you database over time – add tables, add columns, remove tables or columns, reorganize – referred to as the schema of the database onUpgrade method for class that extends SQLiteOpenHelper – for converting database on device (from previous version of your app) to scheme used by newer version of app – not trivial! 29

Aside - Contract Class If you plan to use the database in multiple activities and components of your app – consider creating a Contract Class A class with constants that define table names and columns – instead of hard coding in multiple places Recall the built in ContactsContract and CalendarContract classes 30

Databases on Device can pull database and view data/data/app package/database sqlitebrowser is a decent tool 31

sqlite browser Entire Database: – Recall, we created a single table 32

sqlite browser ratings table 33

sqlite Manager for Firefox Alternative to sqlite Viewer 34

Inserting Data ContentValues: object with key/value pairs that are used when inserting/updating databases Each ContentValue object corresponds to one row in a table _id being added and incremented automatically 35

Inserting Data In AddEditRating When save button clicked 36

Inserting Data In DatabaseConnector 37 nullColumnHack, for inserting empty row

More on insert The second parameter nullColumnHack – that's the parameter identifier "optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values (second parameter) is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty." 38

Updating Data In AddEditRating When save button clicked notice id added 39

Updating Data In DatabaseConnector 40

Query Data Getting a single row by _id – in order to populate ViewRating 41

Query Data Get all rows To populate the ListView in the MovieRaterActivity only getting _id and name columns 42

Cursors When you execute a query on a database in Android … you get a Cursor back "Cursor provided random [access] read- write access to the result of a query" Commonly used in other database implementations / models 43

Cursor find out number of rows in result with getCount() iterate over rows – moveToFirst(), moveToNext() determine column names with getColumnNames() get values for current row 44

Cursor To use all the data … wrap the Cursor in a SimpleCursorAdapter pass the Adapter to a ListView or other view to handle lots of data NOTE: result must contain an integer column named _ID that is unique for the result set – used as id for row in ListView 45

Database Connection Recall: 46

MovieRaterActivity Rating Adapter is a CursorAdapter from onCreate method 47

Updating Cursor Cursor initially null separate task to create cursor and update adapter 48

Async Task Again 49

Clicking on Item in List _id not displayed but still part of entry in list -> use _id to get back to database row 50

Deleting Data Menu Option in ViewRating 51

Other Cursor Options moveToPrevious getCount getColumnIndexOrThrow getColumnName getColumnNames moveToPosition getPosition 52

Possible Upgrades Add functionality to – show all movies that share a particular genre – movies from a date range – shared tags – table for the genres (predefined) Simply more complex data base queries 53

ALTERNATIVES TO SQLITE MOVING HIGHER UP THE FOOD CHAIN 54

Alternatives to sqlite When using SQLite you may feel like you are "Down in the weeds" Various alternatives to work higher up the food chain – in other words at a higher level of abstraction Object Relational Mappers - ORM Higher level wrappers for dealing with sql commands and sqlite databases Many ORMs exist 55

ORM Example - Sugar ORM Syntactic Sugar? – what does that mean? Install package Add to manifest file Classes you want stored in database must extend SugarRecord 56

Example ORM - Sugar ORM 57

Example ORM - Sugar ORM CRUD operations – create, read, update, destroy – working with the data 58

Example ORM - Sugar ORM 59

Implications for Movie Rater Simple syntax and method calls to make queries on the database In the demo app, Movie Rating should be its own class Could use Sugar ORM to simplify dealing with the sqlite database 60