Android Developer Fundamentals V2

Slides:



Advertisements
Similar presentations
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
Advertisements

Android course Database dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.
CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
SQLite 1 CS440. What is SQLite?  Open Source Database embedded in Android  SQL syntax  Requires small memory at runtime (250 Kbytes)  Lightweight.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 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
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Android Content Providers and Intents.
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.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
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.
Persistence Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
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.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
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.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
CHAPTER 9 File Storage Shared Preferences SQLite.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
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
Database Programming Code Dissection. Layered Approach Presentation (Activity) DbSampleActivity.java DataAccess (DataSource) CommentsDataSource.java MySQLiteHelper.java.
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Android aplikacija i baze podataka
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
SQLite in Android Landon Cox March 2, 2017.
Data Definition and Data Types
Mobile Software Development for Android - I397
Mobile Application Development BSCS-7 Lecture # 18, 19
CIS 136 Building Mobile Apps
ListView: Part 2.
Android Application SQLite 1.
Reactive Android Development
Android Database using SQLite
Data Definition and Data Types
Applied CyberInfrastructure Concepts Fall 2017
Lecture 8: Database Topics: Basic SQLite Operations.
Database Management  .
SQL – Application Persistence Design Patterns
Chapter 5: Menus, SQLite.
Mobile Computing With Android ACST Android Database Storage Part 2
CMPE419 Mobile Application Development
What is a Database? A collection of data organized in a manner that allows access, retrieval, and use of that data.
CIS 136 Building Mobile Apps
CMPE419 Mobile Application Development
Mobile Computing With Android ACST 4550 Android Database Storage
Chapter 4 Introduction to MySQL.
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.
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Programming Dr. Mohsin Ali Memon.
SQL – Application Persistence Design Patterns
Presentation transcript:

Android Developer Fundamentals V2 Storing Data

Contents SQLite Database Queries

This is only a refresher This course assumes that you are familiar with Databases in general SQL databases in particular SQL query language

SQLite Database

SQL Databases Store data in tables of rows and columns (spreadsheet…) Fields contain data, references to other fields, or references to other tables Rows are identified by unique IDs Column names are unique per table

Tables WORD_LIST_TABLE _id word definition 1 "alpha" "first letter" 2 "beta" "second letter" 3 "particle"

SQLite software library Implements SQL database engine that is self-contained (requires no other components) serverless (requires no server backend) zero-configuration (does not need to be configured for your application) transactional (changes within a single transaction in SQLite either occur completely or not at all)

Queries

SQL basic operations Insert rows Delete rows Update values in rows Retrieve rows that meet given criteria

SQL Query SELECT word, description FROM WORD_LIST_TABLE WHERE word="alpha“ Generic SELECT columns FROM table WHERE column="value"

Sample queries 1 SELECT * FROM WORD_LIST_TABLE Get the whole table 2 SELECT word, definition FROM WORD_LIST_TABLE WHERE _id > 2 Returns [["alpha", "particle"]]

More sample queries 3 SELECT _id FROM WORD_LIST_TABLE WHERE word="alpha" AND definition LIKE "%art%" Return id of word alpha with substring "art" in definition [["3"]] 4 SELECT * FROM WORD_LIST_TABLE ORDER BY word DESC LIMIT 1 Sort in reverse and get first item. Sorting is by the first column (_id) [["3","alpha","particle"]]

Last sample query 5 SELECT * FROM WORD_LIST_TABLE LIMIT 2,1 Returns 1 item starting at position 2. Position counting starts at 1 (not zero!). Returns [["2","beta","second letter"]]

rawQuery() String query = "SELECT * FROM WORD_LIST_TABLE"; rawQuery(query, null); query = "SELECT word, definition FROM WORD_LIST_TABLE WHERE _id> ? "; String[] selectionArgs = new String[]{"2"} rawQuery(query, selectionArgs);

query() SELECT * FROM WORD_LIST_TABLE WHERE word="alpha" ORDER BY word ASC LIMIT 2,1; Returns: [["alpha", "particle"]] String table = "WORD_LIST_TABLE" String[] columns = new String[]{"*"}; String selection = "word = ?" String[] selectionArgs = new String[]{"alpha"}; String groupBy = null; String having = null; String orderBy = "word ASC" String limit = "2,1" query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

Cursors Queries always return a Cursor object Cursor is an object interface that provides random read-write access to the result set returned by a database query ⇒ Think of it as a pointer to table rows

SQLiteOpenHelper A helper class to manage database creation and version management. public class MySQLiteDataBase extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

SQLiteOpenHelper public class MySQLiteDataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "MyDB"; @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

SQLiteOpenHelper public class MySQLiteDataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "MyDB"; public MySQLiteDataBase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }

Create and Upgrade DataBase public void onCreate(SQLiteDatabase db) { String CREATE__TABLE = "CREATE TABLE words ( id INTEGER PRIMARY KEY AUTOINCREMENT,word TEXT, meaning TEXT )"; db.execSQL(CREATE__TABLE); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE words"); this.onCreate(db); }

SQLite Data Types INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value. REAL: The value is a floating point value, stored as an 8-byte IEEE floating point number. TEXT: The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) BLOB: The value is a blob of data, stored exactly as it was input.

Add a Record ContentValues class is used to store a set of key- values public void add(String w, String m){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("word", w); values.put("meaning",m); db.insert("words", null, values); db.close(); }

Delete Record public void clear(int id ) { SQLiteDatabase db = this.getWritableDatabase(); db.delete("words","id=?",new String[]{id+""}); db.close(); } public void clearAll() { SQLiteDatabase db = this.getWritableDatabase(); db.delete("words",null,null); db.close(); }

Query a record public String get(int id){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from words where id=?",new String[]{id+""}); if (cursor != null && cursor.moveToFirst()){ String ret=""; ret=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; db.close(); return ret; } else {db.close(); return "Record not Found.\n";} }

Records List public String list(){ SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query("words",new String[]{"id","word","meaning"}, null, null, null, null, null, null); if (cursor != null && cursor.moveToFirst()){ String ret=""; ret=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; while(!cursor.isLast()){ cursor.moveToNext(); ret+=cursor.getString(0)+" : "+ cursor.getString(1)+":"+cursor.getString(2)+"\n"; } db.close(); return ret; } else return "Data Base Is Empty.\n"; }

Object of DB class MySQLiteDataBase db=new MySQLiteDataBase(this); db.clear(3);

END