Monday, 9 February 2015

SQLiteDatabase Part1: Create database Example

Database part1: creating database and it’s schema
Step 1:  create new project
Step 2: create a java file by extending SQLiteOpenHelper class
Step 3: provide the implementation for the OpenHelper class as shown below
As OpenHelper is an abstract class we need to provide implementation for two abstract methods of it(onCreate() and onUpgrade()) and call super class constructor by passing four parameters they are
1.      Context value(Context object)
2.      Database name(String)
3.      CursorFactory(null)
4.      Database Version code(int)
Write the following code in SQliteOpenHelper subclass
DBOpenHelper.java
package com.swamys.database.openhelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

       // database name
       private final static String DATABASE_NAME = "sample_db";

       // datbase version code
       private final static int DATABASE_VERSION_CODE = 1;

       // table name
       public final static String SAMPLE_TABLE_NAME = "sample_table";

       // table creation
       private final String CREATE_SAMPLE_TABLE = "create table "
                     + SAMPLE_TABLE_NAME
                     + "(_id integer primary key autoincrement, name text, phone text);";

       public DBOpenHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION_CODE);
       }

       @Override
       public void onCreate(SQLiteDatabase db) {
              db.execSQL(CREATE_SAMPLE_TABLE);
       }

       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

       }

}

Step 4: Create Adapter to communicate with OpenHelper class
And write the following code in Adapter class
DBAdapter.java
 package com.swamys.database.dbadapter;

import com.swamys.database.openhelper.DBOpenHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {
      
       SQLiteDatabase database;
      
       public DBAdapter(Context context) {
              DBOpenHelper openHelper = new DBOpenHelper(context);
              database = openHelper.getWritableDatabase();
       }

       public void closeConnection(){
              database.close();
       }
      
}

Step 5: Create Object for Adapter from MainActivity by passing Context value
MainActivity.java
package com.swamys.databaseexample;

import android.app.Activity;
import android.os.Bundle;

import com.swamys.database.dbadapter.DBAdapter;

public class MainActivity extends Activity {

       DBAdapter dbAdapter;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              dbAdapter = new DBAdapter(getApplicationContext());
       }

       @Override
       protected void onDestroy() {
              super.onDestroy();
              dbAdapter.closeConnection();
       }

}

Step 6: Run your app and go to DDMS perspective and select respective Devise (emulator) and go to data in File Explorer again data under that, go to your app package and then go to databases there you can see a sample_db file

Screen Shot:



No comments:

Post a Comment