Wednesday, January 8, 2020

Android Unit 3 : Database connectivity using SQLite for B.C.A., B.Sc.IT. and Other IT Students.



--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
E-Mail : udayshah80@gmail.com
Contact No : 7600044051



Unit 3 : Database connectivity using SQLite


Question :  Android Data and Storage UPI
Answer :     Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications and the user and how much space your data requires.
Your data storage options are the following:
Shared Preferences       :        Store private primitive data in key-value pairs.
Internal Storage            :        Store private data on the device memory.
External Storage           :        Store public data on the shared external storage.
SQLite Databases         :        Store structured data in a private database.
Network Connection    :        Store data on the web with your own network server.

Android provides a way for you to expose even your private data to other applications with a content provider. A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose.

Question:   Explain Intro to Database connectivity using SQLite
·        Android provides a several way to store user and application data to save information in an application.
·        SQLite is one way for storing user data.
·        SQLite is very light weight database which comes with android as most of the non travel application also need to save user settings and some application to manage large amount of information in file and database. SQLite is an open source database. SQLite support standard relation.
·        Database feature like SQL syntax transaction and prepaid statement.
·        The database requires limited memory at run time. It approximate 250 kilo bytes is default require memory to embedded database file into the device at runtime.
·        SQLite support the data type like text integer real in it.
·        Text is like a string in java.
·        Integer is similar to long in a java and real is similar to double in Java.
·        All other types must be converted into one of this field before getting saved in the database.
·        SQLite itself does not validate if the type written to the column are actually of the define type.
·        It means user can write integer into string column.
·        SQLite is embedded into every android device.
·        Using and SQLite database in android does not require a setup procedure or administrator of the database.
·        User have to define SQL statement for creating and update in the data base after word the database is automatically manage for user on the android platform to access SQLite database must access the file system.
·        Which can be store the user data as well as application data inside the device?
·        If your application create database then database is by default saved In the directory.
·        According to SQLite architecture the android database.package must import because it content all necessary class for working with database.
·        The android.database.SQLite package content a specific class for SQLite.
·        To create and update a database In your android application then you have to create a subclass of the SQLite open helper class.
·        In the constructor of your sub class call super () method of SQLite open helper class.
·        In this class you need to override following method to create and update your database. That is

·        1. On create () method
·        2. On update () method
·        Both methods are receiving SQLite database object as a parameter which is represent in java using the database.
·        SQLite database is base class for working with SQLite database in android and provide a various method to manipulate.
·        Database like open database, close database, update database query to the database.
·        It means SQLite database provides insert method, update method and delete method.
·        In addition to SQLite database provide execSQL () method which allow to execute an SQL statement directly.
·        The object content value allows defining key value in a pair.
·        The key represent table column identifier and value represent the content for the table record in this column.
·        Content value can be use for insert, update of database entry.
·        Query can be created using the run query () method and query () method with the help of SQLite builder class.
·        Row query () method directly accept and SQL statement as a input query () method provide structure interface for specific the SQL query.
·        SQLite query builder is a convening class that helps to built SQL query.
Save Files
·        Android used a file system that is similar to disk based file system on other platform.
·        A file object is shied for reading or writing large amount of data into the device.
·        Android device allow two different storage area that is internal/external storage.
·        This means that android offer build in known volatile memory structure for internal storage while some removable storage used external partition of the storage area.
·        So there are always to storage space and API behavior is the same weather it is internal removable or not.
·        External storage area is not available always because user can mount external storage area as a USB storage etc.
·        When user save the file to internal storage it is automatically store in appropriate directory in a file system using the following two method.
·        GetfilesDir() method
·        GetcatchDir() method
GetfileDir() method return a file which is represent in and internal directory while getcacheDir()  method return file represent In an internal directory for your application.

Save to file to external storage device
·   Android application support a limited storage device inside the hand set so it is compulsory to set external storage capacity into the device because of the external storage maybe unavailable so user has to amount an storage to a PC or remove the SD card that is provided for external storage.
·     User can store a file or the media into external storage state by calling get external storage state () method.
·        There are 2 types of category of your external storage is
·        Public file
·        Private file
·      Public file means a file that should freely available to other application and to the user.
·        When user uninstalls any application the file should be remain available for user.
·        E.g. photo capture by your application.
·    Private files are the files that is protected by your application and automatically delete the file when user uninstall your application.
·    Although the file are technically accessible by the user sometimes it is use for running the application and specially design for that application.
·      Additional resources are download in your application or a temporary media file is example of private file .
·   When the user uninstall your application the system automatically delete all the file in your application from external private directory.


Question : Sharing Data between Applications with Content Providers

  • On the Android platform, one application cannot directly access read or write other application's data.
  • All persistent data of an app is private to that app.
  • Every application has its own id data directory and own protected memory area.
  • This means that an app cannot query or manipulate the data of another app.
  • However, if you want to enable an app to query or manipulate the data of another app, you need to use the concept of content providers.


Content Provider :

  • Content provider is a set of data wrapped up in a custom API to read and write.
  • It acts as an interface that allows you to store and retrieve data from a data source. 
  • And also allows you to share an app’s data with other apps.
  • Content providers decouple the app layer from the data layer by abstracting the underlying data source, thereby making apps data-source independent.
  • They allow full permission control by monitoring which app components or users should have access to the data making data sharing easy.
  • As a result, any app with appropriate permissions can add, remove, update, and retrieve data of another app including the data in some native Android databases.


There are 2 types of Content Providers:

Custom content providers: These are created by the developer to suit the requirements of an app.
Native content providers: These provide access to built-in databases, such as contact manager, media player, and other native databases. You need to grant the required permissions to your app before using native content providers.
Eg: The WhatsApp app sharing mobile contacts data and Media store-Allows other applications to access, store media files.




Above diagram shows how content provider works. Application 2 stores its data in its own database and provides a provider. Application 1 communicates with the provider to access Application 2's data.

Content Provider uses a special URI starting with content:// will be assigned to each content providers and that will be recognized across applications.

Creating a Content Provider :

To create a content provider we need to

Create sub class for ContentProvider.
Define content URI
Implement all the unimplemented methods. insert(), update(), query(), delete(), getType().
Declare the content provider in AndroidManifest.xml

https://myshopprime.com/uday.shah3/shop

Please share and comment it... Thank you.

:: Best of Luck ::