SQLite. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 6: Working with Databases. What is a Database Server. Advantages of SQLite

Similar documents
Database Development In Android Applications

07. Data Storage

The Suggest Example layout (cont ed)

This lecture. The BrowserIntent Example (cont d)

Lab 3. Accessing GSM Functions on an Android Smartphone

Initialising the Views (GameFragment) 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 5: Ultimate Tic-Tac-Toe Game: The Interface (Part III)

O X X X O O X O X. Tic-Tac-Toe. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 2: The Ultimate Tic-Tac-Toe Game

SQLite Database. References. Overview. Structured Databases

An Android Studio SQLite Database Tutorial

Android: Data Storage

The Basis of Data. Steven R. Bagley

Mobile and Ubiquitous Computing: Android Programming (part 4)

Android User Interface

Eng. Jaffer M. El-Agha Android Programing Discussion Islamic University of Gaza. Data persistence

CS371m - Mobile Computing. Persistence - SQLite

CS378 -Mobile Computing. Persistence -SQLite

Exercise 1: First Android App

Mobile Computing Practice # 2d Android Applications Local DB

The drawable/tile empty.xml file

Android Programming Lecture 15 11/2/2011

Android Components. Android Smartphone Programming. Matthias Keil. University of Freiburg

Android Components Android Smartphone Programming. Outline University of Freiburg. Data Storage Database University of Freiburg. Notizen.

Android User Interface Android Smartphone Programming. Outline University of Freiburg

Orientation & Localization

Debojyoti Jana (Roll ) Rajrupa Ghosh (Roll ) Sreya Sengupta (Roll )

CSCU9YH: Development with Android

Accelerating Information Technology Innovation

Lab 1 - Setting up the User s Profile UI

SharedPreference. <map> <int name="access_count" value="3" /> </map>

Android Programming Lecture 16 11/4/2011

Data storage and exchange in Android

Sqlite Update Failed With Error Code 19 Android

How-to s and presentations. Be prepared to demo them in class. ons

ListView Containers. Resources. Creating a ListView

Mobile Application Development Android

MOBILE CLIENT FOR ONLINE DICTIONARY. Constantin Lucian ALDEA 1. Abstract

Mobile Programming Lecture 10. ContentProviders

CSC 337. Relational Databases and SQL. Rick Mercer

Dependence Graph Considerering Resource for Automatic Sample Program Generation

Mobile Application Development Android

Unit 1 - Chapter 4,5

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

Database and MySQL Temasek Polytechnic

MOBILE APPLICATIONS PROGRAMMING

Developing Android Applications Introduction to Software Engineering Fall Updated 1st November 2015

SQL Commands & Mongo DB New Syllabus

CMSC436: Fall 2013 Week 3 Lab

Mobile User Interfaces

PERSİSTENCE OBJECT RELATİON MAPPİNG

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

CS143: Relational Model

EMBEDDED SYSTEMS PROGRAMMING SQLite

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Mobile Application Development Android

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

App Development for Smart Devices. Lec #5: Content Provider

Mobile Computing Practice # 2c Android Applications - Interface

MySQL: an application

Mr. Pritesh N. Patel Assistant Professor MCA ISTAR, V. V. Nagar ANDROID DATABASE TUTORIAL

(Refer Slide Time: 1:12)

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

Lab 5 Periodic Task Scheduling

This document contains information on fixed and known limitations for Test Data Management.

Lecture 1. Monday, August 25, 2014

Now go to bash and type the command ls to list files. The unix command unzip <filename> unzips a file.

ELET4133: Embedded Systems. Topic 15 Sensors

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Lecture 8. Database vs. Files SQL (I) Introduction to SQL database management systems (DBMS)

Learn about Android Content Providers and SQLite

Lab 4 In class Hands-on Android Debugging Tutorial

Required Core Java for Android application development

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Enhancement of Open Source Monitoring Tool for Small Footprint Databases

1. Implementation of Inheritance with objects, methods. 2. Implementing Interface in a simple java class. 3. To create java class with polymorphism

Programming the Database

Oracle Login Maximum Length Of Column Name In 11g Table

Android Tutorial: Part 3

CS 4330/5390: Mobile Application Development Exam 1

Fundamentals of Web Programming

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Announcements. PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read. Take a break around 10:15am

CS 250 SQL and SQLite. Benjamin Dicken

Developing Android Applications

COMP4521 EMBEDDED SYSTEMS SOFTWARE

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Useful tools for an effcient development of an Android App - The Android SDK

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial

Creating a User Interface

SharedPreferences Internal Storage External Storage SQLite databases

Introduction To JAVA Programming Language

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

COMP 430 Intro. to Database Systems. Encapsulating SQL code

MTAT Introduction to Databases

Android Programming (5 Days)

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Transcription:

SQLite 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 6: Working with Databases Dr Dimitris C. Dracopoulos SQLite is a tiny yet powerful database engine. Besides Android, it can be found in: Apple iphone Symbian phones Mozilla Firefox Skype PHP Adobe AIR Mac OS X Solaris many others... Advantages of SQLite Dimitris C. Dracopoulos 1/26 What is a Database Server Dimitris C. Dracopoulos 2/26 Its free. Its small. The current version is about 150KB. It requires no setup or administration. There is no server, no config file, and no need for a database administrator. A SQLite database is just a file. You can take that file, move it around, and even copy it to another system. Just another server which receives requests from clients requiring access to data in a database (this could be read or write). DATABASE SERVER give me data (or update data) here they are 00 11 01 01 Android stores it in the /data/data/packagename/databases directory. Client Dimitris C. Dracopoulos 3/26 Dimitris C. Dracopoulos 4/26

Relational Databases Accessing Databases Everything organised into tables. Name Age Position Salary John Smith 35 Manager 40000 Robert Barclay 28 Developer 30000 George Deval 25 Administrator 32000 Tom Bubble 38 Head of Sales 45000 SQL (Structured Query Language) is used. The main variations are: Transact SQL (T-SQL). Used by Microsoft SQL Server and Sybase. The two have very few differences. PL-SQL. Used in Oracle. ANSI SQL. Parts of it adopted by commercial and public domain products. SQL Datatypes Dimitris C. Dracopoulos 5/26 SQL Statements Dimitris C. Dracopoulos 6/26 SQL Datatype CHAR(n) VARCHAR(n) INTEGER or INT DOUBLE DATE TIMESTAMP DECIMAL, NUMERIC Corresponding Java type String String int double java.sql.date java.sql.timestamp java.math.bigdecimal Four main categories: CREATE and INSERT (create a table, put values into it) SELECT (query the database about data matching certain criteria) UPDATE (to change the values in existing rows) DELETE and DROP (to delete specific rows or tables). Dimitris C. Dracopoulos 7/26 Dimitris C. Dracopoulos 8/26

The CREATE Statement The INSERT Statement CREATE TABLE tablename( colname datatype ) INSERT INTO tablename (colname1, colname2, colname3...) VALUES (value1, value2, value3,...) CREATE TABLE Person ( name VARCHAR(100), age INTEGER, address VARCHAR(100)) INSERT INTO Person (name, age, address) VALUES ( John Smith, 26, London ), ( Tom Bubble, 34, New York ) Dimitris C. Dracopoulos 9/26 Dimitris C. Dracopoulos 10/26 The SELECT Statement SELECT Name1, Name2, Name3... FROM tablename1, tablename2,... WHERE conditions ORDER BY colnames SELECT Person.name, Person.address, ListensTo.music_group_name FROM Person, ListensTo WHERE ListensTo.music-group_name IN ( Beatles, Popstars ) AND Person.name = ListensTo.person_name AND Person.address = London Dimitris C. Dracopoulos 11/26 The UPDATE Statement UPDATE tablename SET colname1=value1, volname2=value2... WHERE colnamei someoperator valuei UPDATE Person SET age = 25, address= Manchester WHERE name = John Smith Dimitris C. Dracopoulos 12/26

The DELETE and DROP Statements DELETE FROM tablename WHERE colnamei someoperator valuei DELETE FROM Person WHERE name = John Smith The rows corresponding to John Smith are deleted. To delete a whole table (not only the contents but the table itself) use the DROP statement. (after that the table needs to be created again). An example of Creating a Table in SQLite create table mytable ( _id integer primary key autoincrement, name text, phone text ); One of the columns is designated as the PRIMARY KEY, a number that uniquely identifies the row. AUTOINCREMENT means that the database will add 1 to the key for every record to make sure its unique. By convention, the first column is always called id. Unlike most databases, in SQLite the column types are just hints. If you try to store a string in an integer column, or vice versa, it will just work with no complaints. DROP TABLE Person Dimitris C. Dracopoulos 13/26 Dimitris C. Dracopoulos 14/26 Building the SQLite Database The SQLiteOpenHelper Class 1. Check if the database exists 1.1 If it does not, create it, create the tables and populate them with initial data 1.2 If it does, open it, check what version it is. 2. If the database is an old version, upgrade it to a newer version. The above steps are facilitated by using the SQLiteOpenHelper class. 1. SQLiteOpenHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version): create an object of a subclass of the helper class. 2. Call getwritabledatabase() or getreadabledatabase() to create or open a database. 3. oncreate (SQLiteDatabase db): Called when the database is created for the first time. 4. onupgrade (SQLiteDatabase db, int oldversion, int newversion): Called when the database needs to be upgraded (new version). Dimitris C. Dracopoulos 15/26 Dimitris C. Dracopoulos 16/26

A Hello Database Example The main activity corresponds to class MainActivity.java. An interface Constants.java defining some constants: package org. example. events ; import android. provider. BaseColumns ; public interface Constants extends BaseColumns { public static final String TABLE_ NAME = " events " ; // Columns in the Events database public static final String TIME = " time " ; public static final String TITLE = " title " ; Class EventsData: package org. example. events ; import static android. provider. BaseColumns. _ID ; import static org. example. events. Constants. TABLE_ NAME ; import static org. example. events. Constants. TIME ; import static org. example. events. Constants. TITLE ; import android. content. Context ; import android. database. sqlite. SQLiteDatabase ; import android. database. sqlite. SQLiteOpenHelper ; public class EventsData extends SQLiteOpenHelper { private static final String DATABASE_ NAME = " events.db"; private static final int DATABASE_ VERSION = 1; Dimitris C. Dracopoulos 17/26 Dimitris C. Dracopoulos 18/26 /* Create a helper object for the Events database */ public EventsData ( Context ctx ) { super ( ctx, DATABASE_NAME, null, DATABASE_ VERSION ); @Override public void oncreate ( SQLiteDatabase db) { db. execsql (" CREATE TABLE " + TABLE_ NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME + " INTEGER," + TITLE + " TEXT NOT NULL );"); @Override public void onupgrade ( SQLiteDatabase db, int oldversion, int newversion ) { db. execsql (" DROP TABLE IF EXISTS " + TABLE_ NAME ); oncreate (db ); The main.xml file: <? xml version =" 1.0 " encoding ="utf -8"?> < ScrollView xmlns:android =" http: // schemas. android. com / apk / res / android " android: orientation =" vertical " android: layout_ width =" fill_ parent " android: layout_ height =" fill_ parent " > < TextView android:id ="@+id/ text " android: layout_ width =" fill_ parent " android: layout_ height =" wrap_ content " /> </ ScrollView > Dimitris C. Dracopoulos 19/26 Dimitris C. Dracopoulos 20/26

The Events class: package org. example. events ; import static android. provider. BaseColumns. _ID ; import static org. example. events. Constants. TABLE_ NAME ; import static org. example. events. Constants. TIME ; import static org. example. events. Constants. TITLE ; import android. app. Activity ; import android. content. ContentValues ; import android. database. Cursor ; import android. database. sqlite. SQLiteDatabase ; import android.os. Bundle ; import android. widget. TextView ; public class MainActivity extends Activity { private static String [] FROM = { _ID, TIME, TITLE,; private static String ORDER_BY = TIME + " DESC "; private EventsData events ; @Override public void oncreate ( Bundle savedinstancestate ) { super. oncreate ( savedinstancestate ); setcontentview (R. layout. main ); events = new EventsData ( this ); try { addevent (" Hello, Android!"); Cursor cursor = getevents (); showevents ( cursor ); finally { events. close (); Dimitris C. Dracopoulos 21/26 private void addevent ( String string ) { /* Insert a new record into the Events data source. You would do something similar for delete and update. */ SQLiteDatabase db = events. getwritabledatabase (); ContentValues values = new ContentValues (); values. put ( TIME, System. currenttimemillis ()); values. put ( TITLE, string ); db. insertorthrow ( TABLE_NAME, null, values ); private Cursor getevents () { /* Perform a managed query. The Activity will handle closing and re - querying the cursor when needed. */ SQLiteDatabase db = events. getreadabledatabase (); Cursor cursor = db. query ( TABLE_NAME, FROM, null, null, null, null, ORDER_BY ); return cursor ; The showevents method: private void showevents ( Cursor cursor ) { // Stuff them all into a big string StringBuilder builder = new StringBuilder ( " Saved events :\n"); while ( cursor. movetonext ()) { /* Could use getcolumnindexorthrow () to get indexes */ long id = cursor. getlong (0); long time = cursor. getlong (1); String title = cursor. getstring (2); builder. append (id ). append (": "); builder. append ( time ). append (": "); builder. append ( title ). append ("\n"); cursor. close (); // Display on the screen TextView text = ( TextView ) findviewbyid ( R.id. text ); text. settext ( builder ); Dimitris C. Dracopoulos 22/26 Dimitris C. Dracopoulos 23/26 Dimitris C. Dracopoulos 24/26

Dimitris C. Dracopoulos 25/26 The sqlite3 utility You can examine and manipulate the files representing the databases in an emulator (or a rooted device): adb shell At the shell prompt: sqlite3 /data/data/org.db.databaseexample/databases/events.db SQLite version 3.8.10.2 2015-05-20 18:17:19 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite> (org.db.databaseexample should be the package name of your application). sqlite>.tables android_metadata events sqlite> select * from events; 1 1488209719643 Hello, Android! 2 1488209728651 Hello, Android! 3 1488209733013 Hello, Android! 4 1488209737209 Hello, Android! 5 1488209740765 Hello, Android! Dimitris C. Dracopoulos 26/26