CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas.

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
SQLite in Mobile Apps by Dan Youberg. Overview Many well known applications and Internet browsers use SQLite due to its very small size (~250 Kb). Also.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Chapter 8 : Transaction Management. u Function and importance of transactions. u Properties of transactions. u Concurrency Control – Meaning of serializability.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Transaction Management WXES 2103 Database. Content What is transaction Transaction properties Transaction management with SQL Transaction log DBMS Transaction.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 8A Transaction Concept.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
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.
Databases Dan Otero Alex Loddengaard
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
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.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 13 Managing Databases with SQL Server 2000.
ASP.NET Programming with C# and SQL Server First Edition
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Introduction. 
ANDROID CONTENT PROVIDERS Peter Liu School of ICT, Seneca College.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
PHP meets MySQL.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Introduction to Database Management Systems. Information Instructor: Csilla Farkas Office: Swearingen 3A43 Office Hours: Monday, Wednesday 4:15 pm – 5:30.
1cs Intersection of Concurrent Accesses A fundamental property of Web sites: Concurrent accesses by multiple users Concurrent accesses intersect.
Creating and using Persistent Data From before – Where does the data come from? – Why is it kept? – How is it used? Affects design and implementation choices.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 136 Database Systems I SQL Modifications and Transactions.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Introduction to Database Systems1. 2 Basic Definitions Mini-world Some part of the real world about which data is stored in a database. Data Known facts.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
Introduction to Database Management Systems. Information Instructor: Csilla Farkas Office: Swearingen 3A43 Office Hours: Monday, Wednesday 2:30 pm – 3:30.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Introduction.  Administration  Simple DBMS  CMPT 454 Topics John Edgar2.
SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.
Session 1 Module 1: Introduction to Data Integrity
1 Intro stored procedures Declaring parameters Using in a sproc Intro to transactions Concurrency control & recovery States of transactions Desirable.
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.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
CHAPTER 9 File Storage Shared Preferences SQLite.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
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,
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
3 A Guide to MySQL.
Mobile Applications (Android Programming)
SQLite in Android Landon Cox March 2, 2017.
Android Application SQLite 1.
Database JDBC Overview CS Programming Languages for Web Applications
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Mobile Computing With Android ACST 4550 Android Database Storage
Android Developer Fundamentals V2
Chapter 11 Managing Databases with SQL Server 2000
Department of School of Computing and Engineering
SQLLite and Android.
Mobile Programming Dr. Mohsin Ali Memon.
Advanced Topics: Indexes & Transactions
Presentation transcript:

CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas

SQL DBs and Android If your app needs to store and retrieve data in a flexible way Consider using a relational DB Helps ensure data integrity – Well designed relations cannot store self-inconsistent data Standard SQL language – Very powerful for sorting and selecting the data needed For simple apps using SQL is harder work than simply writing data in JSON format to file system – But worth the effort when appropriate

Relational Modelling Relation == table Each column specifies the column name and type Database Schema (loosely speaking) – The column names and types for each table Each row is a single record of data with a value for each column – Depending on settings, cell entries may be NULL Dilemmas sometimes arise regarding how far to normalise a table

Exercise How would you model the following Contact DB? – Each person is identified by name and primary address – Each person may have a number of telephones (home, office, mobile etc.) When designing an App be prepared to compromise: – Perfect normalisation versus realistic usage – Higher normal forms can sometimes be less efficient

SQLite Separate open-source project On Android platform as standard – Hence default choice of relational DB for Android – Other choices of pure Java DB also possible – But would add to size of App Standalone DB – Does not run as a separate server process – Supports most but not all SQL statements Transactional

Transactions Help ensure that DB is always kept in a consistent state Each transaction maps the state of the data from one consistent state to the next ACID properties

Atomicity A transaction may involve a set or sequence of updates Atomicity ensures that either that ALL happen, or NONE of them happen Enclose sequence between begin transaction and end transaction statements Example: imagine the phone battery died during a transaction – All the statements executed so far are held in temporary storage – And only committed at the end (e.g. by moving a file pointer)

Consistency The database is always kept consistent Helped by: – Suitably high normal form – Other transactional properties: Atomicity and Isolation

Isolated When concurrent threads are hitting the DB – Or data is being processed in App using concurrent threads Must ensure that threads do not interfere – (see example in Threads and Surface Views lecture notes) Isolated / Isolation property guarantees this – May be achieved using locking mechanisms such as Semaphores, or Java statements such as synchronized

Durable An obvious property! Once made a transaction should be saved permanently And not be affected by systems crashes or power loss

Package: android.database.sqlite Contains the SQLite database management classes that an application can use to manage its own private database Also has the sqlite3 database tool in the tools/ folder Use this tool to browse or run SQL commands on the device. Run by typing sqlite3 in a shell window.

SQLite versus JDBC Android has a set of non-JDBC classes to work directly with SQLite However, JDBC drivers are also available for SQLite – See: – Hence another possible option would be to use a JDBC driver – This offers better standardisation, and could be worth exploring – For these notes we’re going to stick with the Android API

Accessing DBs from Code A number of approaches possible Can embed SQL strings in App – And make DB calls from wherever needed Can use an automated / semi-automated tool such as Spring / Hibernate Or: – Define a special data handling class – All DB access is via handler – Main App code only sees objects, never SQL strings Discussion question: which way is best?

DB Interface / Helper Approach Define all DB access methods in an interface Then provide one or more implementations of this as required e.g.: – SQLite Implementation: the real thing – DummyDB: implement all the methods but don’t actually save the data persistently – DummyDB can be very useful for testing the App DBInterface SQLiteImpl.DummyDB

Exercise Write a DB interface class called ScoreDBInterface to support the storage and retrieval of all scores for a game Interface should allow storage of each score as it occurs And retrieval of high score, and list of top N scores Each score record consists of: – Time Stamp of type long – Player Name of type String – Score achieved of type int Write the method signatures and also any convenience classes that you need

Android Approch Similar to DBInterface approach Defines data contract class – This includes an inner class for each table defining its column names – Methods to create tables and drop them – Methods to execute queries See Android Developer example – storage/databases.html storage/databases.html Also CE881 Lab code, outlined below

SQLite in Android Construct SQL strings Execute SQL strings to perform actions Also use specific helper methods such as query() – These take arguments to build a SELECT clause with Or use rawQuery() – pass the SQL string Both query() and rawQuery() return a Cursor object This is used to iterate over the resulting table of rows

Score DB Example adapted from FeedReader example: ScoreDBContract – Class used to define table names – Has inner classes for each table – In this case, just one table called entry (for entries in the score table) – All columns defined in abstract class ScoreEntry ScoreHelper class – Manages access to the DB – Declares types for each table column – Methods for creating and dropping tables – May also implement ScoreDBInterface

Snippets from ScoreHelper private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + " (" + ScoreDBContract.ScoreEntry._ID + " INTEGER PRIMARY KEY," + ScoreDBContract.ScoreEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP + ScoreDBContract.ScoreEntry.COLUMN_NAME_PERSON + TEXT_TYPE + COMMA_SEP + ScoreDBContract.ScoreEntry.COLUMN_NAME_SCORE + “INTEGER” + " ) "; private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME; public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); }

Adding an Entry… SQLiteDatabase db = scoreHelper.getWritableDatabase(); scoreHelper.addEntry(db, "Simon", "" + 10 * (1 + random.nextInt(100))); Log.i(TAG, "Populated Table"); db.close();

The addEntry method

Using a Cursor with a Query Selecting all scores Cursor cursor = db.rawQuery("Select * from " + ScoreDBContract.ScoreEntry.TABLE_NAME, new String[]{}); cursor.moveToFirst(); // need to find index of each column before retrieving it int scoreIndex = cursor.getColumnIndex(ScoreDBContract.ScoreEntry.COLUMN_NAME_SCORE); while (!cursor.isAfterLast()) { int score = cursor.getInt(scoreIndex); boolean flag = cursor.moveToNext(); Log.i(TAG, flag + " : " + score); }

More details … See SQLite lab exercise – Including.zip with all the details The example opens and closes a DB Connection each time it is needed This is perhaps not the most efficient way – But it saves thinking through ifecycle methods More efficient way: – open connection when activity is created – Close connection when activity is destroyed

Summary Android ships with SQLite database Natural choice for storing data that must be flexibly queried And have guaranteed integrity However, API is rather low level Best approach is to embed all DB access code in a separate helper class (or classes) – My favourite: all access goes through a DB interface class – Can then test rest of app using a dummy implementation Work through lab exercise