Database in Android

Hey! Everyone today we are going to study one of the most important topic in android i.e Database.Database is the place in which we can store Data and can manipulate it.
For example in this post we are going to make a small android program on database,we are going to make database for a general store so here we go…

To make a Database for any data firstly we must have to make an separate class in program where we have to extends the SQLiteOpenHelper and have to implements all it’s methods as you can see bellow..

Above you can see the Database class in which we can give the name to our Database.As for example we name it as Store and it’s version is 1.
In Activity_main.xml we can make the design as per our need as you can see it bellow i.e..

Now after completing all this process we have to move to our main portion i.e MainActivity.java where we have to complete the programming section bellow you can see the code for the remaining portion.

public class MainActivity extends AppCompatActivity {
EditText Name,Code,Price;
TextView tvmassage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name=(EditText)findViewById(R.id.Name);
        Code=(EditText)findViewById(R.id.Code);
        Price=(EditText)findViewById(R.id.Price);
        tvmassage=(TextView)findViewById(R.id.tvmassage);
        try{
            Database helper=new Database(this);
            SQLiteDatabase database=helper.getWritableDatabase();
            database.execSQL("create table Store(name text,code integer primary key,price integer)");
        }
        catch(Exception ex)
        {
         System.out.println(ex);
        }
    }
    public void Insert(View v)
    {
        try
        {
           Database helper =new Database(this);
           SQLiteDatabase database=helper.getWritableDatabase();
            ContentValues values=new ContentValues();
            String name=""+Name.getText();
            int  code=Integer.parseInt(""+Code.getText());
            int price=Integer.parseInt(""+Price.getText());
            values.put("name",name);
            values.put("code",code);
            values.put("price",price);
            long n=database.insert("Store",null,values);
           tvmassage.setText("Item inserted");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }

    }
    public void Delete(View v)
    {
        try{
            Database helper =new Database(this);
            SQLiteDatabase database=helper.getWritableDatabase();
            ContentValues values=new ContentValues();
            int  code=Integer.parseInt(""+Code.getText());
            values.put("code",code);
            String []para={""+Code.getText()};
            long n=database.delete("Store","code=?",para);
            tvmassage.setText("Item Deleted");

        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
        }

    }
    public  void Search(View v)
    {
        try{
            Database helper=new Database(this);
            SQLiteDatabase database = helper.getReadableDatabase();
            ContentValues values=new ContentValues();
            String code=""+Code.getText();
            String column[]={"name","price"};
            String para[]={code};
            Cursor cursor=database.query("Store",column,"code=?",para,null,null,null);
            if(cursor.moveToFirst())
            {
                String name=cursor.getString(0);
                String price=cursor.getString(1);
                Name.setText(name);
                Price.setText(price);
            }
            else {
                Name.setText("Not found");
                Price.setText("Not found");
            }
            tvmassage.setText("Item searched");
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    public void Update(View v)
    {
        try {
            Database helper=new Database(this);
            SQLiteDatabase database=helper.getWritableDatabase();
            String name=""+Name.getText();
            int price=Integer.parseInt(""+Price.getText());
            String code=""+Code.getText();
            ContentValues values=new ContentValues();
            values.put("name",name);
            values.put("price",price);
            values.put("code",code);
            String []para={code};
            long n=database.update("Store",values,"code=?",para);
            tvmassage.setText("Item updated");
        }

         catch (Exception ex)
         {
             System.out.println(ex.getMessage());
         }
        }
    }

So here we are in above portion You can see the code for the inserting,deleting,searching and updating.These are the manipulations which we can do on a Database.

             
Thank you! for your precious time hope you like this post meet you all soon.
Bye…Bye…

Leave a Reply