SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas.
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
 data/data-storage.html#pref data/data-storage.html#pref 
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.
Objectives Connect to MySQL from PHP
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Android course Database dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
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.
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
Database Rung-Hung Gau Department of Computer Science and Engineering
Data Storage: Part 3 (SQLite)
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Concepts of Database Management Seventh Edition
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;
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.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
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.
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.
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.
1. Playing with SQLite Database  SQLite : Database specific name for Android Application  For windows there are several kind of database name : Mysql,
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.
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.
Database Programming Code Dissection. Layered Approach Presentation (Activity) DbSampleActivity.java DataAccess (DataSource) CommentsDataSource.java MySQLiteHelper.java.
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,
CS499 – Mobile Application Development
Making content providers
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Sqlite primer.
Android Content Providers & SQLite
Mobile Applications (Android Programming)
Data Storage: Part 4 (Content Providers)
SQLite in Android Landon Cox March 2, 2017.
Mobile Software Development for Android - I397
Mobile Application Development BSCS-7 Lecture # 18, 19
Mobile Applications (Android Programming)
Android Application SQLite 1.
Reactive Android Development
CS499 – Mobile Application Development
Android Database using SQLite
Mobile Application Development Chapter 5 [Persistent Data in Android]
Mobile Computing With Android ACST Android Database Storage Part 2
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CMPE419 Mobile Application Development
CS371m - Mobile Computing Persistence - SQLite.
CMPE419 Mobile Application Development
Mobile Computing With Android ACST 4550 Android Database Storage
Android Developer Fundamentals V2
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.
Presentation transcript:

SQLite Database

SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within one file – Can be seen in file explorer /data/data/packagename/database/ Self-contained – no special administration needed

SQLite Primary components – SQLiteOpenHelper android.database.sqlite.SQLiteOpenHelper manages aspects of the database – creating or opening database – SQLiteDatabase android.database.sqlite.SQLiteDatabase – methods to interface with database via SQL – Cursor android.database.Cursor – provides access to result set returned by a query

Typical Approaches Approach 1: Providing a database with the app – store.db file in Assets folder.db file created within Android.db file created in other SQLite manager, such as Firefox plug-in – in onCreate() of extended Activity, copy the database if it does not already exist in the device’s internal memory – onCreate() method in extended SQLiteOpenHelper class typically empty Approach 2: Database created when app first run – onCreate() method in extended SQLiteOpenHelper class contains SQL to create database – will only run if the database does not exist

SQLiteOpenHelper class

SQLiteOpenHelper Abstract class – Subclass in a new.java file – Useful methods onCreate (abstract) – called when DB first created – table creation done here onUpgrade (abstract) – called when DB is changed (i.e. during new app release) – essentially used to drop and recreate database getReadableDatabase – called to get a read-only version of database getWriteableDatabase – called to get an editable version of database

Sample code - SQLiteOpenHelper public class MyClass extends SQLiteOpenHelper { public MyClass (Context ctx) { //Parameters: Context, file name, CursorFactory factory, database version) //CursorFactory rarely used – only used for customized Cursors super (ctx, "myfilename.db", null, 1); } public void onUpgrade (SQLiteDatabase db, int oldVer, int newVer) { db.execSQL("DROP TABLE IF EXISTS TABLE_NAME"); onCreate(db); } public void onCreate(SQLiteDatabase db) { //_id field must be included!!! db.execSQL("CREATE TABLE MYTABLE (_id INTEGER PRIMARY KEY AUTOINCREMENT,..."); }

SQLiteDatabase class

SQLiteDatabase Concrete class – SQL methods Convenience methods – query – returns a Cursor – insert – returns _id of new record or –1 if error occurred – update – returns number of rows affected – delete – number of rows affected (0 if no where clause) » pass ‘1’ as where clause to get count in this case Manual creation of SQL – rawQuery » can execute any SELECT statement and returns a Cursor – execSQL » can execute any SQL statement that is not a SELECT void return type

SQLiteDatabase query method – Parameters String – table name String [] – columns to select – _id field must be included if displaying in List String – where clause (without the word WHERE) – null if not wanted – ? for arguments String [] – selection clause, to allow arguments for field names in where clause – only if ? in preceding string String – group by clause (without the words GROUP BY) String – having clause (without the word HAVING) String – order by clause (without the words ORDER BY)

Sample code – query String [] FROM = {List of desired columns to select}; SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getReadableDatabase(); Cursor myCursor = db.query(MY_TABLE_NAME, FROM, null, null, null, null, null);

SQLiteDatabase insert method – Parameters String – table name String – work around to allow insertion of empty row in SQL – nullColumnHack – SQLLite does not allow: » INSERT INTO MYTABLE – ContentValues will be null ContentValues – Object holding the corresponding field names and values

Sample code – insert SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, column value);... values.put(COLUMN_NAME, column value); db.insert(MY_TABLE_NAME, null, values);

SQLiteDatabase update method – Parameters String – table name ContentValues – Object holding the corresponding field names and values String – where clause (without the word WHERE) String [] – selection clause, to allow arguments for field names in where clause

Sample code – update SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase(); String where = "My WHERE clause (without the word WHERE)"; ContentValues values = new ContentValues(); values.put(COLUMN_NAME, column value);... values.put(COLUMN_NAME, column value); db.update(MY_TABLE_NAME, values, where, null);

SQLiteDatabase delete method – Parameters String – table name String – where clause (without the word WHERE) String [] – selection clause, to allow arguments for field names in where clause

Sample code – delete SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase(); String where = "My WHERE clause (without the word WHERE)"; db.delete(MY_TABLE_NAME, where, null);

SQLiteDatabase rawQuery method – Parameters String – SQL statement String [] – selection clause, to allow arguments for field names in where clause

Sample code – rawQuery SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase(); Cursor myCursor = db.rawQuery(“ SQL statement here”, null);

SQLiteDatabase execSQL method – Parameters String – SQL statement

Sample code – execSQL SQLiteDatabase db = myInstanceOfSQLiteOpenHelper.getWritableDatabase(); db.execSQL(“SQL statement here”);

Cursor class

Aspects of Cursor class SQLiteDatabase query method returns a Cursor – Cursor must be managed by Activity Cursor class allows manipulation of pointer – move, moveToFirst, moveToNext, moveToPosition, etc. Cursor class allows retrieval of data – getInt, getDouble, getString, etc. – must provide column position Cursor can be associated with an Adapter – typically SimpleCursorAdapter

Sample code – Cursor data retrieval Retrieving the integer in the 4 th row, 3 rd column myCursor.moveToPosition(3); int x = myCursor.getInt(2);

Sample code – populating a Cursor from a Database private Cursor populateCursor() { String [] FROM = {List of desired DB field names}; SQLiteDatabase db = myInstanceOfSQLiteHelper.getReadableDatabase(); Cursor myCursor = db.query(MY_TABLE_NAME, FROM, null, null, null, null, null); return myCursor; }

Sample code – displaying Cursor contents in a ListView private void displayRecords(Cursor c) { String [] from = {List of desired field names to display}; int [] to = new int [] {List of TextViews to display field names (i.e. R.id.myTextView)}; ListView lv = (ListView)findViewById(R.id.myListView); SimpleCursorAdapter records = new SimpleCursorAdapter(this, R.layout.my_file_for_TextViews, c, from, to); lv.setAdapter(records); }