Skip to content

Latest commit

 

History

History
161 lines (120 loc) · 7.75 KB

README.md

File metadata and controls

161 lines (120 loc) · 7.75 KB

Sqlite_for _android : Contains Steps , solution for Errors that I faced while implementing it in projects

Steps on how to install sqlite:

  • Download sqlite-tools-win32-*.zip from this link: https://www.sqlite.org/download.html
  • Once you've downloaded this zip, open up Windows explorer and navigate to your C:\ drive. Once there, create a folder called sqlite. Copy the .zip file you just downloaded into this folder and then extract its contents.
  • Once you've done that, you'll need to add C:\sqlite to your system's PATH environment variable. Finally, you'll need to open your command prompt program and type in sqlite3, then hit enter.
  • Video Link : https://www.youtube.com/watch?v=zOJWL3oXDO8

Procedure :

  1. Define a schema & a contract
  2. Create a database using SQL HELPER
  3. Add a cursor to it
  4. Use content provider
  5. Use cursor loader
  6. Use content URI
  7. Use URI matcher
  8. Sanity checking
  9. Implement contentprovider[MIME Type]

Why we need Contract class :

  • To define constants used in the Database.
  • To avoid spelling errors.
  • Ease of updating schema.

Contract class contains :

  • Outer class.
  • Inner class.
  • String constants.

Use of SQLiteOpenHelper :

  • Create a sqliteDB.
  • Gives a connection to the DB.
  • Manages updating DB schema if version changes.

How to implement SQLiteOpenHelper :

  1. Create a class that extends from SQLiteOpenHelper
  2. Create constants for DBnames & DBversions
  3. Create a constructor
  4. Implement onCreate() method
  5. Implement onUpgrade() method

Getting a database connection :

  • use SQLiteDatabase db = mDbHelper.getReadableDatabase();
  • mDbHelper is an instance of SQLiteOpenHelper which will look for the Database , If a DB doesnot exist it will create a new DB using onCreate() method.

Cursor :

Acessing sqlite3 database in Androidstudio : as1

Use of Content provider :

  • Inorder to eliminate the direct interaction of the UI with DB , we use content provider .
  • Doesnot allow the invalid data to enter the database .
  • Good abstraction layer between the data source & the UI code
  • Works well with other android framework classes , For an instance:Activity manager

Use of Cursor loader :

  • The cursor loader will check when any new data is added or removed in the list to keep the list updated
  • It also checks the data and updates it
  • So the cursor loader works in conjuction with the listView & the cursor adapter
  • Loader that queries the ContentResolver with a specific URI & returns a Cursor
  • Loads data on a background thread because reading & writing to a database can be an expensive operation
  • works well with content provider because a loader is tied to a URI
  • Inorder to implement the Cursor loader we requires Content provider

Activities to be done to implement a Cursor loader :

  1. onCreate()[getLoaderManager().initLoader()]
  2. onCreateLoader()
  3. onLoadFinished()[use swapCursor(cursor) in this method]
  4. onLoaderReset()[use swapCursor(null) in this method]

How does other app access database of an another app :

  • The external app uses a Content URI to give a request to the Contacts provider of the required app

How does the Content URI matches with correct content provider :

  • The content Resolver does this work
    • The content URI(content://com.android.contacts/contacts/2) will be passed to the content Resolver
      • Based on the content authority (i.e: com.android.contacts) the content resolver will handle the data and send to ContactsProvider.
      • To allow the external apps to access the database we should give android:exported="true" in the manifest file

Use of Content URI :

  • can point to a part of a database
  • can point to a file(text,images etc..)
  • In this content URI content://com.android.contacts/contacts/2
    • content:// - refers to schema
    • com.android.contacts - refers to Content Authority(Unique for each content provider)
    • contacts - refers to Type of data (table name should be given here)
    • 2 - refers to specific rows in the table(inorder to derive a single row)

Why URI matcher is needed :

  • Helps us to ensure that the Contentprovider doesn't try to handle unexpected content URIs

Steps to create a URI matcher :

  • Setup the URI matcher with the URI patterns your content provider will accept & assign each pattern an integer code
  • Call UriMatcher.match(uri) and pass in a uri , which will return the corresponding integer code (if matched)

Sanity checking :

Why to declare MIME Type :

Implement this [method] to handle requests for the MIME type :

  • The returned MIME type should start with “vnd.android.cursor.item” for a single record, or “vnd.android.cursor.dir/” for multiple items.
  • Step 1: Declare MIME Type constants in Contract.java file
  • Step 2: Implement ContentProvider getType() method

Related to UI :

Error:class or interface expected:

  • when there is an error as class or interface expected it can be cleared by removing the curly brace on where it is showing a hint as an error

Projects done using Sqlite:

Web links :

Code reference :

Sql injection : https://www.netsparker.com/blog/web-security/sql-injection-vulnerability/