SQlite database in android

Application:->java->package->new->java class
file->mydatabase.java
import java.util.ArrayList/**  * Created by Rahul on 15-Jul-16.  */ public class mydatabase extends  SQLiteOpenHelper {//SQLiteOpenHelper=inbuilt class     public static final String table_name “college_details”;     public static final String column_college_id “id”;     public static final String column_college_name “name”;     public static final String db_name “ptu”;     public static final int db_version = 1;     public static final String query_create “create table ” table_name ” ( ” column_college_id ” Integer(10) primary key, ” column_college_name ” Text)”; //declaring variables for database along with some keywords so that these are accessible in other files of the project.     @Override     public void onCreate(SQLiteDatabase db) {         db.execSQL(query_create); //this will execute on creation of project when it runs for first time.    }     @Override     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//when changes takes place in database like updation of item(s)it will execute     }     public mydatabase(Context c) { //parametrized constructor//Context in Android is an interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.Simple Example
//Every boss has an assistant to look after, to do all less important and time consuming tasks. If a file or a cup of coffee is needed, assistant is on the run. Some bosses barely know what’s going on in the office, so they ask their assistants regarding this too. They do some work themselves but for most other things they need help of their assistants.
In this scenario,
  • Boss –  is the Android application
  • Assistant – is context
  • Files/Cup of coffee  – are resources
Putting this in simple terms
We generally call context when we need to get information about different parts of our application like Activities, Applications etc.
Some operations(things where assistant is needed) where context is involved:
  1. Loading common resources
  2. Creating dynamic views
  3. Displaying Toast messages
  4. Launching Activities etc.
Different ways of getting context:
  • getContext()
  • getBaseContext()
  • getApplicationContext()

Comments

Popular posts from this blog

Documentation of project has been fabricated

Alert Dialog Boxes