Friday, June 29, 2012

Create Database

These are many way to create database in android application.
When your application install only that time database is created and it deleted when you remove your application from device.

For this you need know about SQLiteOpenHelper.

Here is small demo of how to create database and how to use it.

1.  Create class that extends SQLiteOpenHelper class in that create sub-class of table data that you need in onCreate() method you write code for table creation.

public class Database extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "database.db";
    private static final int DATABASE_VERSION = 1;
    SQLiteDatabase sqlitedatabase = null;

    public Database(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public static final class EMP {
        public final static String TABLE_NAME = "employee";

        public final static String _ID = "_id";
        public final static String NAME = "emp_name";

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        sqlitedatabase = db;

        sqlitedatabase.execSQL("create table " + EMP.TABLE_NAME + "(" + EMP._ID
                + " Integer primary key autoincrement," + EMP.NAME + " text);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}



2. Create class that contains all the function that you will use in your application.

public class DatabaseFunction {
    SQLiteDatabase db = null;
    Database database;

    DatabaseFunction(Context context) {
        database = new Database(context);
        db = database.getWritableDatabase();

    }

    public long insertData(String name)
    {
        ContentValues cv = new ContentValues();
        cv.put(EMP.NAME    , name);
        return db.insert(EMP.TABLE_NAME, null, cv);
    }
   
    public int deleteData(String name)
    {
        return db.delete(EMP.TABLE_NAME, EMP.NAME+"=?", new String[]{name});
    }
   
    public int updateDate(String oldName, String newName)
    {
        ContentValues cv =new ContentValues();
        cv.put(EMP.NAME, newName);
        return db.update(EMP.TABLE_NAME, cv, EMP.NAME+"=?", new String[]{oldName});
    }
    public Cursor selectFromEmp() {
        return db.query(EMP.TABLE_NAME, null, null, null, null, null, null);
    }

    // close SQLiteDatabase object it must
    public void close() {
        db.close();
    }

}

3. How to use above database in your application
    Create object of DatabaseFunction class that contains your function that you want to use in your application

using object of DatabaseFunction call appropriate method.


public class EmpActivity extends Activity{

@Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emp);
        DatabaseFunction df=new DatabaseFunction(getApplicationContext()) ;
       
        String name = "abc";
        df.insertData(name);
        Cursor c=df. selectFromEmp();
        // perform any operation on cursor
        if( c!=null && c.getCount()>0)
        {
            c.moveToFirst();
            String nm = c.getString(c.getColumnIndex(EMP.NAME));
            Log.i("Name", nm);
        }
//         then close cursor object first
       c.close();
      
       String newName = "xyz";
      
       // for updating table
       df.updateDate("abc", newName);
       c=df. selectFromEmp();
       // display updated values from cursor
       if( c!=null && c.getCount()>0)
       {
           c.moveToFirst();
           String nm = c.getString(c.getColumnIndex(EMP.NAME));
           Log.i("Name", nm);
       }
      
       // delete value from table
       df.deleteData(newName);
      
//     then close DatabaseFunction object
       df.close();
    }

}

Thank you for referring my blog.
Please write comment for any suggestion and improvement for writing blog.






1 comment:

  1. put clear content by commenting or highlighting main things like creation of table or database etc

    ReplyDelete