Skip to content

An easy to use Android Application that demonstrate the use of data persistence in android i.e., saving the data in a permanent way such that it remains on your device even after closing the activity.

Notifications You must be signed in to change notification settings

churchil15/Pets-Database

Repository files navigation

Pets-Database

An easy to use Android Application that demonstrate the use of data persistence in android i.e., saving the data in a permanent way such that it remains on your device even after closing the activity.

This app can be used by any local pets authority in order to keep track on pets available in their shelter. There's a floating action button (FAB) that allows us to add details like pets name, breed, gender and weight.

SQLite DataBase

The database that runs on android is SQLite database which was used to store, update or delete the details of the pets in this app. But there also can be different types of data storage options to store data in Android like,

  1. Files: These are good for saving large media files but are not efficient.
  2. Shared Preference: These are good for in-app user preferences and somewhat efficient but are used better for single, important piece of information bascially small data.
  3. SQLite databases: SQLite database are highly efficient and are good for organizing a lot of related and structured data for easy access. SQLite databases are divided into tables, and these tables can have rows and coloumns

Steps for Creating a Database in Android

  1. Create a Package that will manage database specific classes.

  2. Create a Schema and a Contract Class.

    Now in order to create an Contract we should first create an outer class named BlankContract and then an inner class named BlankEntry for each table in the database. Each of these inner classes should implement a class called BaseColumns. String constants for table name and for each of the headings. Lastly also create constants for gender and use them in there classes where needed (in context with this app).

  3. Implement another Android class (SQLiteOpenHelper) that will help us create the database in SQLite.

    i) Create a class that extends from SQLiteOpenHelper.

    ii) Create constants for database name and database version.

    iii) Create a constructor

    iv) Implement the onCreate() method -> this method is for when the database is first created. This is where the creation and initial population of the tables will happen

    v) Implment the onUpgrade() method -> this method is used for when the database schema of the database changes. Ex:- adding a different coloumn. onUpgrade() simply drop the database table and recreates it. The purpose of this method is that it gives you the opportunity to update the database files based on the changes that you've made to the structure in your code.

  4. Put Information into a database

    Make a Content Value class object and then call the put methods with the associated coloumn name and coloumn value. Important thing to note here is that this information doesn't actually insert anthing into our database just yet. All this is doing is creating an object, with the coloumn rows and associated values. Bascially dummy data display krwane ke liye.

    To actually create a new row in the database we will need to use the insert method from the SQLiteDatabase class.

  5. Read information from a database

    Same as there exist an SQLiteDatabase insert() method, there's also an SQLiteDatabase query() which helps you avoid any syntax errors by helping you structure your query. This is a better alternative then the rawquery() method which is a similar API used to read the database using pure SQL.

    In order to use the query() method, we have to define it's parameters (projection, selection, and selectionArgs are the 3 particular parameters that need to be defined). Projections simply means the coloumns that we are interested in getting back, selection and selectionArgs deal with the optional WHERE clause.

Some Terminologies

  1. Contract Class: The primary reason developers need contracts is to have one place to document the structure of their database also known as a schema. When generating SQL commands, it removes the possibility of introducing spelling errors. Using the contract makes it wasier to update the data schema.

  2. Content Providers: One reason of using content providers is to ensure that the data we're inserting into the database is valid. Content Provider acts as a layer between the activity and database. Another reason of using content providers is that it works well with other Android framwork classes and allows us to easily share data with other apps.

  3. Content URI: A content URI is a URI that identifies data in a provider.

  4. Content Resolver: Content Resolver takes the URI and resolves which content provider should handle our request.

  5. URI Matcher: Matches a URI with an integer code. Helps us ensure that the ContentProvider doesn't try to handle unexpected content URIs.

  6. Adapter: Responsible for making a list item view for each item in data source.

    If our data source is ArrayList such as an array list of words, then we're going to use a ArrayAdapter. If the data source is a cursor such as a cursor of pets, then we're going to use a CursorAdapter.

About

An easy to use Android Application that demonstrate the use of data persistence in android i.e., saving the data in a permanent way such that it remains on your device even after closing the activity.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages