Skip to main content

Application to Create, Insert, update and Delete operation on the database



Q12.Create an application to Create, Insert, update and Delete operation on the database

File consists :

1.activity_main.xml
2.Main_Activity.java
3.kDatabaseAdapter.java
4.Message.java

activity_main.xml



<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto"     
xmlns:tools="http://schemas.android.com/tools"     
android:layout_width="match_parent"     
android:layout_height="match_parent"     
tools:context="com.haldar.kishan.progm12.MainActivity"
 
<EditText         
android:id="@+id/user"         
android:layout_width="wrap_content"         
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true"         
 android:layout_marginTop="12dp" 
 android:ems="15"         
android:inputType="textPersonName"         
android:hint="username" /> 

<EditText 
 android:id="@+id/pass"         
android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
 android:layout_alignLeft="@+id/user" 
 android:layout_alignStart="@+id/user" 
 android:layout_below="@+id/user"         
 android:layout_marginTop="18dp" 
  android:ems="14" 
  android:inputType="textPersonName" 
  android:hint="password" 
  android:layout_alignRight="@+id/name" 
  android:layout_alignEnd="@+id/name" />

  <Button 
   android:id="@+id/adduser"         
   android:layout_width="match_parent"         
   android:layout_height="wrap_content"         
   android:layout_marginTop="15dp" 
   android:text="add user" 
   android:onClick="addUser" 
   android:layout_below="@+id/pass"         
   android:layout_alignLeft="@+id/name" 
   android:layout_alignStart="@+id/name"         
   android:layout_alignRight="@+id/user" 
   android:layout_alignEnd="@+id/user" />

    <Button         
     android:id="@+id/ViewAllDetail"         
     android:layout_width="match_parent"         
     android:layout_height="wrap_content" 
     android:onClick="viewDetails" 
     android:layout_marginTop="20dp" 
     android:text="View All Details" 
     android:layout_below="@+id/adduser" 
     android:layout_alignLeft="@+id/pass" 
     android:layout_alignStart="@+id/pass" 
     android:layout_alignRight="@+id/adduser" 
     android:layout_alignEnd="@+id/adduser" />

    <EditText 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"         
      android:layout_marginTop="23dp"         
      android:ems="15" 
      android:inputType="textPersonName" 
      android:hint="Enter the value For Any Operation" 
      android:layout_below="@+id/ViewAllDetail"         
      android:layout_alignParentRight="true"         
      android:layout_alignParentEnd="true"         
      android:layout_marginRight="32dp" 
      android:layout_marginEnd="32dp" />

    <Button         
     android:id="@+id/button"         
     android:onClick="getDetails" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"         
     android:text="get details"         
     android:layout_marginTop="15dp" 
     android:layout_below="@+id/name" 
     android:layout_alignLeft="@+id/ViewAllDetail"         
     android:layout_alignStart="@+id/ViewAllDetail" 
     android:layout_alignRight="@+id/ViewAllDetail"         
     android:layout_alignEnd="@+id/ViewAllDetail" />

    <Button 
     android:id="@+id/update"         
     android:onClick="update" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"         
     android:layout_alignLeft="@+id/button"         
     android:layout_alignStart="@+id/button"         
     android:layout_below="@+id/button" 
     android:layout_marginTop="12dp" 
     android:text="update" />

    <Button 
     android:id="@+id/delete" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="delete"         
     android:onClick="delete"         
     android:layout_alignBaseline="@+id/update" 
     android:layout_alignBottom="@+id/update"         
    android:layout_toRightOf="@+id/update" 
    android:layout_toEndOf="@+id/update"         
    android:layout_marginLeft="14dp"         
    android:layout_marginStart="14dp" />

    <Button         
     android:id="@+id/clear"         
     android:onClick="clearRec"         
     android:layout_width="wrap_content"         
     android:layout_height="wrap_content"         
     android:text="clear" 
     android:layout_below="@+id/update"         
     android:layout_alignLeft="@+id/update" 
     android:layout_alignStart="@+id/update" 
     android:layout_marginTop="13dp" />
</RelativeLayout>

MainActivity.java


package com.haldar.kishan.progm12;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
EditText userName,password,myname;
    kDatabaseAdapter myhelper;
    String user1,pass1;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userName=(EditText)findViewById(R.id.user);
        password=(EditText)findViewById(R.id.pass);
        myname=(EditText)findViewById(R.id.name);

        myhelper=new kDatabaseAdapter(this);
    }

    public void addUser(View view) {
        user1 = userName.getText().toString();
        pass1 = password.getText().toString();
        if (user1.matches("") || pass1.matches("")) {
            Message.message(this, "Enter the all required field");
        } else {

            long id = myhelper.insert(user1, pass1);
            if (id < 0) {
                Message.message(this, "Unsuccessful");
            } else {
                Message.message(this, "successful Inserted Row");
            }
        }
    }

    public void viewDetails(View view) {
        String dataMsg=myhelper.getAllData();
        Message.message(this,dataMsg);
    }

    public void getDetails(View view) {
        String s1 = myname.getText().toString();
        //kishan 123456        if (s1 == null || s1.trim().equals("")) {
            Message.message(this, "Please Enter the Name for Details");
        } else {
            String mydata = myhelper.getData(s1);
            Message.message(this, mydata);
        }
    }

    public void  update(View view) {

        String s1 = myname.getText().toString();
        //kishan 123456        if (s1 == null || s1.trim().equals("")) {
            Message.message(this, "Please Enter Two Name Variable as oldName NewName");
        } else {
            String oldName = s1.substring(0, s1.indexOf(" "));
            String newName = s1.substring(s1.indexOf(" ") + 1);
            int count = myhelper.updateName(oldName, newName);
            Message.message(this, "" + count);
        }
    }


    public void delete(View view) {
        String s1 = myname.getText().toString();
        if (s1 == null || s1.trim().equals("")) {
            Message.message(this, "Please Enter the Name for Delete");
        } else {
            int count = myhelper.deleteRow(s1);
            Message.message(this, "" + count);
        }
    }

    public void clearRec(View view) {
      myhelper.clearAll();
        Message.message(this,"All Rows deleted Successfully");

    }
}

kDatabaseAdapter.java


package com.haldar.kishan.progm12;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/** * Created by SISTER on 15-04-2017. */
public class kDatabaseAdapter {
  kHelper myhelper;
    public  kDatabaseAdapter(Context context){
     myhelper=new kHelper(context);
    }

    public long insert(String name,String password){
        SQLiteDatabase db=myhelper.getWritableDatabase();
        ContentValues contentval=new ContentValues();
        contentval.put(kHelper.NAME,name);
        contentval.put(kHelper.PASSWORD,password);
        long id=db.insert(kHelper.TABLE_NAME,null,contentval);
        return id;
    }

    public  String getAllData(){
        SQLiteDatabase db=myhelper.getWritableDatabase();
        String[] columns={kHelper.UID,kHelper.NAME,kHelper.PASSWORD};
        Cursor pointer=db.query(kHelper.TABLE_NAME,columns,null,null,null,null,null);
       StringBuffer myBuffer=new StringBuffer();
       while (pointer.moveToNext()){
           int cid=pointer.getInt(0);
           String name=pointer.getString(1);
           String password=pointer.getString(2);
           myBuffer.append(cid+ " "+name+" "+password+"\n");
       }
       return myBuffer.toString();
    }

    public String getData(String name){
       //select name,password from kTable where name=? AND password=?; 
 SQLiteDatabase db=myhelper.getWritableDatabase();
        String[] columns={kHelper.UID,kHelper.NAME,kHelper.PASSWORD};
        String[] selectionArgs={name};
        Cursor pointer=db.query(kHelper.TABLE_NAME,columns,
                kHelper.NAME +" =?",
                selectionArgs,null,null,null);
        StringBuffer myBuffer=new StringBuffer();
        while (pointer.moveToNext()){
            int index0=pointer.getColumnIndex(kHelper.UID);
            int index1=pointer.getColumnIndex(kHelper.NAME);
            int index2=pointer.getColumnIndex(kHelper.PASSWORD);
           int personId=pointer.getInt(index0);
           String PersonName=pointer.getString(index1);
            String PersonPassword=pointer.getString(index2);
            myBuffer.append(personId+" "+PersonName+" "+PersonPassword+"\n");
        }
         return myBuffer.toString();
    }

   public int updateName(String oldName,String newName){
     //UPDATE kTable SET NAME='kishan' where Name=? test        
SQLiteDatabase db=myhelper.getWritableDatabase();
       ContentValues contentVal=new ContentValues();
       contentVal.put(kHelper.NAME,newName);
       String[] whereArg={oldName};
       int count=db.update(kHelper.TABLE_NAME,contentVal,kHelper.NAME+"=?",whereArg);
       return count;
   }

   public int deleteRow(String name){
       //DELETE * FROM kTable where name='kishan'        
SQLiteDatabase db=myhelper.getWritableDatabase();
       String[] whereArgs={name};
       int count= db.delete(kHelper.TABLE_NAME,kHelper.NAME+"=?",whereArgs);
       return count;
   }

    public void clearAll(){
        SQLiteDatabase db=myhelper.getWritableDatabase();
        db.delete(kHelper.TABLE_NAME,null,null);
    }

    static class kHelper  extends SQLiteOpenHelper{
        private static final String DATABASE_NAME="khDatabase.db";
        private static final String TABLE_NAME="khTable";
        private static final int DATABASE_VERSION=3;
        private static final String UID="_id";
        private static final String NAME="Name";
        private static final String PASSWORD="Password";
        private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"
("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),"+PASSWORD+" VARCHAR(255))";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS" +TABLE_NAME;
        private Context context;

        public kHelper(Context context){
            super(context,DATABASE_NAME,null,DATABASE_VERSION);
            this.context=context;
            Message.message(context,"Constructor Called");
        }
        @Override        public void onCreate(SQLiteDatabase db) {
            //CREATE TABLE khTable(_id,INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255)); 
 try {
                db.execSQL(CREATE_TABLE);
                Message.message(context,"OnCreate Called");
            } catch (SQLException e) {
                Message.message(context,""+e);
            }

        }

        @Override        public void onUpgrade(SQLiteDatabase db, int i, int i1) {
            try {
                db.execSQL(DROP_TABLE);
                onCreate(db);
                Message.message(context,"Onupgrade called");
            } catch (SQLException e) {
                Message.message(context,""+e);
            }
        }
    }
}


Message.java


package com.haldar.kishan.progm12;

import android.content.Context;
import android.widget.Toast;

/** * Created by SISTER on 15-04-2017. */
public class Message {
    public static void message(Context context,String msg){
        Toast.makeText(context,msg,Toast.LENGTH_LONG).show();
    }
}
 
 

Comments

Popular posts from this blog

Sum of Digit is Pallindrome or not

  Sum of Digit is Pallindrome or not Given a number N.Find if the digit sum(or sum of digits) of N is a Palindrome number or not. Note:A Palindrome number is a number which stays the same when reversed.Example- 121,131,7 etc. Example 1: Input: N=56 Output: 1 Explanation: The digit sum of 56 is 5+6=11. Since, 11 is a palindrome number.Thus, answer is 1. Example 2: Input: N=98 Output: 0 Explanation: The digit sum of 98 is 9+8=17. Since 17 is not a palindrome,thus, answer is 0. Your Task: You don't need to read input or print aything.Your Task is to complete the function isDigitSumPalindrome() which takes a number N as input parameter and returns 1 if the Digit sum of N is a palindrome.Otherwise it returns 0. Expected Time Complexi...

Menu with 5 option & selected option should appear in text box

Q6. Create a menu with 5 options and selected option should appear in text box. Android Program : Activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.kishanhaldar.program6.MainActivity">                 ...

Android Activity Lifecycle Phases

Q2. Create an application to display various android activity lifecycle phases. Android Program :    MainActivity.java package com.kishanhaldar.implicit_intent; package com.example.hp.myhello; import android.content.res.Configuration; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity {       String msg = "Android :";     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Log.d(msg, "On created Method()");     }     protected void onStart() {         super.onStart(); ...