SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1.

Slides:



Advertisements
Similar presentations
Programming with Android: Data management
Advertisements

Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
CE881: Mobile and Social Application Programming Flexible Data Handling with Integrity: SQLite Simon M. Lucas.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
 data/data-storage.html#pref data/data-storage.html#pref 
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
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.
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.
MySql In Action Step by step method to create your own database.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
Content providers Accessing shared data in a uniform way 1Content providers.
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.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Stored Procedures, Transactions, and Error-Handling
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
Introduction to MySQL Lab no. 10 Advance Database Management System.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
Visual Programing SQL Overview Section 1.
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.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
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.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
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.
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.
Introduction to pysqlite
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
Making content providers
Data Storage: Part 3 (SQLite)
Cosc 5/4730 Sqlite primer.
Mobile Applications (Android Programming)
SQL – Python and Databases
Android Application Data Storage 1.
SQLite in Android Landon Cox March 2, 2017.
CIS 136 Building Mobile Apps
Mobile Applications (Android Programming)
Android Application SQLite 1.
Android Database using SQLite
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Computing With Android ACST Android Database Storage Part 2
CIS 136 Building Mobile Apps
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.
Lecture 8: Database Topics: Basic SQLite Operations.
Mobile Programming Dr. Mohsin Ali Memon.
SQL NOT NULL Constraint
JDBC II IS
Presentation transcript:

SQLite DB Storing Data in Android RAVI GAURAV PANDEY 1

Storing Data on Android Platform Four ways of storing data  Preferences  SQLite Database  Files  Network Preferences –  Basically used for storing user preferences for a single application or across applications for a mobile. This is typically name-value pairs accessible to the context. Databases –  Android supports creating of databases based on SQLite db. Each database is private to the applications that creates it Files –  Files can be directly stored on the mobile or on to an extended storage medium. By default other applications cannot access it. Network –  Data can be stored and retrieved from the network too depending on the availability.  If an application wants to store and retrieve data for its own use, without having to share the data across applications, it can access the SQLite DB directly. There is no need of a content provider. RAVI GAURAV PANDEY 2

SQLite DB Operations  Create a database (typically a one time activity)  Create a table (typically a one time activity)  Insert values into the table  Retrieve the values from the table  Display the retrieved values as a List view  Delete all the records from the table before closing the connection to the database RAVI GAURAV PANDEY 3

SQLite DataTypes  This is quite different than the normal SQL data types so please read: RAVI GAURAV PANDEY 4 Storage ClassDescription NULLThe value is a NULL value. 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) BLOBThe value is a blob of data, stored exactly as it was input.

Affinity Types  SQLite supports the concept of type affinity on columns. Any column can still store any type of data but the preferred storage class for a column is called its affinity. Each table column in an SQLite3 database is assigned one of the following type affinities RAVI GAURAV PANDEY 5 AffinityDescription TEXT This column stores all data using storage classes NULL, TEXT or BLOB. NUMERICThis column may contain values using all five storage classes. INTEGER Behaves the same as a column with NUMERIC affinity with an exception in a CAST expression. REAL Behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation NONE A column with affinity NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another.

SQLite Packages & Classes ( android.database.sqlite ) The “sqlite” Contains the SQLite database management classes that an application would use to manage its own private database.  SQLiteCloseable - An object created from a SQLiteDatabase that can be closed.  SQLiteCursor - A Cursor implementation that exposes results from a query on a SQLiteDatabase.  SQLiteDatabase - Exposes methods to manage a SQLite database.  SQLiteOpenHelper - A helper class to manage database creation and version management.  SQLiteProgram - A base class for compiled SQLite programs.  SQLiteQuery - A SQLite program that represents a query that reads the resulting rows into a CursorWindow.  SQLiteQueryBuilder - a convenience class that helps build SQL queries to be sent to SQLiteDatabase objects.  SQLiteStatement - A pre-compiled statement against a SQLiteDatabase that can be reused. RAVI GAURAV PANDEY 6

android. database. sqlite. SQLiteDatabase  Contains the methods for: creating, opening, closing, inserting, updating, deleting and quering an SQLite database  These methods are similar to JDBC but more method oriented than what we see with JDBC (remember there is not a RDBMS server running) RAVI GAURAV PANDEY 7

1. Creating a Database SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVA TE, null); This opens a database defined in the constant SAMPLE_DB_NAME, if it already exists. Else it creates a database and opens it. The second parameter is operating mode : MODE_PRIVATE meaning it is accessible to only this context. The other modes are and MODE_WORLD_WRITABLE. MODE_WORLD_READABLE RAVI GAURAV PANDEY 8

Database Properties  Important database configuration options include: version, locale, and thread-safe locking. import java.util.Locale; myDatabase.setVersion(1); myDatabase.setLockingEnabled(true); myDatabase.SetLocale(Locale.getDefault()); RAVI GAURAV PANDEY 9

2. Creating a Table sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + SAMPLE_TABLE_NAME + " (LastName VARCHAR, FirstName VARCHAR," + " Country VARCHAR, Age INT(3));"); RAVI GAURAV PANDEY 10

3. Inserting Values sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (‘Tom',‘Patterson',‘UK',28);"); RAVI GAURAV PANDEY 11

4. Retrieve a Value Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " + SAMPLE_TABLE_NAME + " where Age > 10 LIMIT 5", null); if (c != null ) { if (c.moveToFirst()) { do { String firstName = c.getString(c.getColumnIndex("FirstName")); int age = c.getInt(c.getColumnIndex("Age")); results.add("" + firstName + ",Age: " + age); }while (c.moveToNext()); } RAVI GAURAV PANDEY 12

5. Display the values as List this.setListAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_1,results)); The statement displays it as a list as the class extends a ListActivity. RAVI GAURAV PANDEY 13

6. Deleting the values finally { if (sampleDB != null) sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME); sampleDB.close(); } RAVI GAURAV PANDEY 14

QUESTIONS? RAVI GAURAV PANDEY 15