Friday 15 August 2014

android simple example for insert,select,update and delete function



                                               Example for Sqlite Databse


Main Activity


package com.example.crudapps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    Button btn_create,btn_read,btn_update,btn_delete,btn_notify;
   
    Intent intent_call;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       
// Create button declearation and setonclick listener
        btn_create = (Button)findViewById(R.id.btn_creates);
        btn_create.setOnClickListener(this);
       
// Read button declearation and setonclick listener
       
        btn_read = (Button)findViewById(R.id.btn_read);
        btn_read.setOnClickListener(this);
       
// Update button declearation and setonclick listener
       
        btn_update = (Button)findViewById(R.id.btn_update);
        btn_update.setOnClickListener(this);
       
// Delete button declearation and setonclick listener
        btn_delete = (Button)findViewById(R.id.btn_delete);
        btn_delete.setOnClickListener(this);
       
       
        btn_notify = (Button)findViewById(R.id.btn_notification);
        btn_notify.setOnClickListener(this);
       
       
       
    }
   
    @Override
    public void onClick(View v) {
       
        switch(v.getId())
        {
        case R.id.btn_creates:
           
            intent_call = new Intent(MainActivity.this,CreateActivity.class);
            startActivity(intent_call);
           
        break;
        case R.id.btn_read:
           
            intent_call = new Intent(MainActivity.this,ReadActivity.class);
            startActivity(intent_call);
           
            break;
        case R.id.btn_update:
           
            intent_call = new Intent(MainActivity.this,UpdateActivity.class);
            startActivity(intent_call);
           
            break;
        case R.id.btn_delete:
           
            intent_call = new Intent(MainActivity.this,DeleteActivity.class);
            startActivity(intent_call);
           
            break;
        case R.id.btn_notification:
           
            intent_call = new Intent(MainActivity.this,NotificationActivity.class);
            startActivity(intent_call);
           
            break;
        }
             
       
    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


activity_main


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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.example.crudapps.MainActivity" >

    <Button
        android:id="@+id/btn_creates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="83dp"
        android:text="CREATE" />

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_creates"
        android:layout_below="@+id/btn_creates"
        android:layout_marginTop="15dp"
        android:text="READ" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_read"
        android:layout_below="@+id/btn_read"
        android:layout_marginTop="16dp"
        android:text="UPDATE" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_update"
        android:layout_alignRight="@+id/btn_update"
        android:layout_below="@+id/btn_update"
        android:layout_marginTop="27dp"
        android:text="DELETE" />

    <Button
        android:id="@+id/btn_notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_delete"
        android:layout_below="@+id/btn_delete"
        android:layout_marginTop="17dp"
        android:text="Notification" />

</RelativeLayout>


                                            

==================create===========================

CreateActivity.java

package com.example.crudapps;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CreateActivity extends Activity implements OnClickListener {

   
    EditText edit_reg_name,edit_reg_username,edit_reg_password,edit_reg_email,edit_reg_phoneno,edit_reg_address;
    Button btn_reg_reset,btn_reg_register;
   
    private String $_reg_name,$_reg_username,$_reg_password,$_reg_email,$_reg_phoneno,$_reg_address;
   
    SQLiteDatabase sqlite_db = null;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
       
       
        sqlite_db = openOrCreateDatabase("crud.db", MODE_PRIVATE, null);
        String sql_insert = "CREATE TABLE IF NOT EXISTS insert_records(_id integer primary key autoincrement,name varchar(120),username varchar(120),password varchar(120),emailid varchar(120),phoneno varchar(120),address varchar(120))";
        sqlite_db.execSQL(sql_insert);
       

        edit_reg_name = (EditText)findViewById(R.id.edit_reg_name);
       
        edit_reg_username = (EditText)findViewById(R.id.edit_reg_username);
       
        edit_reg_password  = (EditText)findViewById(R.id.edit_reg_password);
       
        edit_reg_email  = (EditText)findViewById(R.id.edit_reg_email);
       
        edit_reg_phoneno  = (EditText)findViewById(R.id.edit_reg_phoneno);
       
        edit_reg_address  = (EditText)findViewById(R.id.edit_reg_address);
       
       
        btn_reg_reset  = (Button)findViewById(R.id.btn_register_reset);
       
        btn_reg_reset.setOnClickListener(this);
       
        btn_reg_register  = (Button)findViewById(R.id.btn_register_register);
       
        btn_reg_register.setOnClickListener(this);

       
    }
   
    @Override
    public void onClick(View v) {
       
        switch(v.getId())
        {
        case R.id.btn_register_register:
           
           
            $_reg_name = edit_reg_name.getText().toString();
            $_reg_username = edit_reg_username.getText().toString();
            $_reg_password = edit_reg_password.getText().toString();
            $_reg_email = edit_reg_email.getText().toString();
            $_reg_phoneno = edit_reg_phoneno.getText().toString();
            $_reg_address = edit_reg_address.getText().toString();
           
           
            if($_reg_name == null || $_reg_name.equals("") || $_reg_username == null || $_reg_username.equals("") || $_reg_password ==null || $_reg_password.equals("") || $_reg_email == null || $_reg_email.equals("") || $_reg_phoneno == null || $_reg_phoneno.equals("") || $_reg_address == null || $_reg_address.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Field is Empty", Toast.LENGTH_SHORT).show();
            }
            else
            {
               
                String inse_product = "insert into insert_records(name,username,password,emailid,phoneno,address) values('"+$_reg_name+"','"+$_reg_username+"','"+$_reg_password+"','"+$_reg_email+"','"+$_reg_phoneno+"','"+$_reg_address+"')";
                sqlite_db.execSQL(inse_product);
                Log.i("================inser proucet",""+inse_product);
                Toast.makeText(getApplicationContext(), "REGISTRATION SUCCESSFULLY REGISTERED", Toast.LENGTH_SHORT).show();
               
                edit_reg_name.setText("");
                edit_reg_username.setText("");
                edit_reg_password.setText("");
                edit_reg_email.setText("");
                edit_reg_phoneno.setText("");
                edit_reg_address.setText("");

            }
           
            break;
        case R.id.btn_register_reset:

                edit_reg_name.setText("");
                edit_reg_username.setText("");
                edit_reg_password.setText("");
                edit_reg_email.setText("");
                edit_reg_phoneno.setText("");
                edit_reg_address.setText("");
           
            break;
           
        }       
       
    }
   
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       
       
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.create, menu);
        return true;
       
       
    }
  
}


activity_create.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_background"
    android:orientation="vertical"
     >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Create "
                    android:gravity="center"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="169dp"
                    android:layout_height="wrap_content"
                    android:text="Name" />

                <EditText
                    android:id="@+id/edit_reg_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" >

                    <requestFocus />
                </EditText>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="username" />

                <EditText
                    android:id="@+id/edit_reg_username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txt_read_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="password" />

                <EditText
                    android:id="@+id/edit_reg_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="emailid" />

                <EditText
                    android:id="@+id/edit_reg_email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/txt_read_emailid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="phonenumber" />

                <EditText
                    android:id="@+id/edit_reg_phoneno"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="address" />

                <EditText
                    android:id="@+id/edit_reg_address"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="19dp" >

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/btn_register_reset"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.50"
                    android:text="Reset" />

                <Button
                    android:id="@+id/btn_register_register"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.50"
                    android:text="Register" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</LinearLayout>



screen



================read actvity========================
ReadActivity.java
package com.example.crudapps;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ReadActivity extends Activity {

    private String $_reg_name,$_reg_email,$_reg_phoneno,$_reg_address;
   
    SQLiteDatabase sqlite_db = null;
    EditText edit_loging_username,edit_loging_password;
    Button btn_read_login;

    TextView txt_read_name,txt_read_email,txt_read_phoneno,txt_read_address;
   
    private String $_loging_username,$_loging_password;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
       
       
        sqlite_db = openOrCreateDatabase("crud.db", MODE_PRIVATE, null);
        String sql_insert = "CREATE TABLE IF NOT EXISTS insert_records(_id integer primary key autoincrement,name varchar(120),username varchar(120),password varchar(120),emailid varchar(120),phoneno varchar(120),address varchar(120))";
        sqlite_db.execSQL(sql_insert);
       
        edit_loging_username = (EditText)findViewById(R.id.edit_read_username);
        edit_loging_password = (EditText)findViewById(R.id.edit_read_password);
       
        txt_read_name = (TextView)findViewById(R.id.txt_read_name);
        txt_read_email = (TextView)findViewById(R.id.txt_read_emailid);
        txt_read_phoneno = (TextView)findViewById(R.id.txt_read_phoneno);
        txt_read_address = (TextView)findViewById(R.id.txt_read_address);
       
        btn_read_login = (Button)findViewById(R.id.btn_read_login);
        btn_read_login.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
               
               
                $_loging_username = edit_loging_username.getText().toString();
                $_loging_password = edit_loging_password.getText().toString();
               
                String sqllite_read = "SELECT * FROM insert_records WHERE username='"+$_loging_username+"' AND password='"+$_loging_password+"'";
                Cursor mcursor = sqlite_db.rawQuery(sqllite_read, null);
               
                if(mcursor.getCount()>0)
                {
                    while(mcursor.moveToNext())
                    {
                        $_reg_name = mcursor.getString(mcursor.getColumnIndex("name"));
                        $_reg_email = mcursor.getString(mcursor.getColumnIndex("emailid"));
                        $_reg_phoneno = mcursor.getString(mcursor.getColumnIndex("phoneno"));
                        $_reg_address = mcursor.getString(mcursor.getColumnIndex("address"));
                    }
                   
                    txt_read_name.setText(""+$_reg_name);
                    txt_read_email.setText(""+$_reg_email);
                    txt_read_phoneno.setText(""+$_reg_phoneno);
                    txt_read_address.setText(""+$_reg_address);
                   
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Invalid User Name and password ", Toast.LENGTH_SHORT).show();
                }
            }
           
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.read, menu);
        return true;
    }
   
   

   
}







 activity_read :




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_background"
    android:orientation="vertical"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="READ DATA FROM DATABASE" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="20dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <EditText
                        android:id="@+id/edit_read_username"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Enter the User Name" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <EditText
                        android:id="@+id/edit_read_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Enter the Password"
                        android:inputType="textPassword" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/btn_read_login"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Login" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:text="View Result" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.50"
                            android:text="Name :" />

                        <TextView
                            android:id="@+id/txt_read_name"
                             android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="name" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView5"
                            android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="Email ID" />

                        <TextView
                            android:id="@+id/txt_read_emailid"
                            android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="Email Id" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView7"
                             android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="Phone No." />

                        <TextView
                            android:id="@+id/txt_read_phoneno"
                             android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="phoneno" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="5dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView9"
                             android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="Address" />

                        <TextView
                            android:id="@+id/txt_read_address"
                             android:layout_width="0dp"
                            android:layout_weight="0.50"
                            android:layout_height="wrap_content"
                            android:text="address" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</LinearLayout>








=================updateActivity=======================



package com.example.crudapps;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateActivity extends Activity implements OnClickListener{

    //widget
   
    EditText edit_update_username,edit_update_password,edit_update_name,edit_update_emailid,edit_update_phoneno,edit_update_address;
    Button btn_update_validate,btn_update_save,btn_update_reset;
   
   
    //location variable Declaration
   
    String $_update_username,$_update_password,$_update_name,$_update_emailid,$_update_phoneno,$_update_address;
   
    SQLiteDatabase sqlite_db = null;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
       
        sqlite_db = openOrCreateDatabase("crud.db", MODE_PRIVATE, null);
        String sql_insert = "CREATE TABLE IF NOT EXISTS insert_records(_id integer primary key autoincrement,name varchar(120),username varchar(120),password varchar(120),emailid varchar(120),phoneno varchar(120),address varchar(120))";
        sqlite_db.execSQL(sql_insert);
       
       
        edit_update_username  = (EditText)findViewById(R.id.edit_update_username);
        edit_update_password  = (EditText)findViewById(R.id.edit_update_password);
        edit_update_name  = (EditText)findViewById(R.id.edit_update_name);
        edit_update_emailid  = (EditText)findViewById(R.id.edit_update_emailid);
        edit_update_phoneno  = (EditText)findViewById(R.id.edit_update_phoneno);
        edit_update_address  = (EditText)findViewById(R.id.edit_update_address);
       
       
        btn_update_validate = (Button)findViewById(R.id.btn_update_validate);
        btn_update_validate.setOnClickListener(this);
       
        btn_update_save = (Button)findViewById(R.id.btn_update_save);
        btn_update_save.setOnClickListener(this);
       
        btn_update_reset = (Button)findViewById(R.id.btn_update_reset);
        btn_update_reset.setOnClickListener(this);
       
       
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
        case R.id.btn_update_validate:
       
            $_update_username = edit_update_username.getText().toString();
            $_update_password = edit_update_password.getText().toString();
           
            String sqllite_read = "SELECT * FROM insert_records WHERE username='"+$_update_username+"' AND password='"+$_update_password+"'";
            Cursor mcursor = sqlite_db.rawQuery(sqllite_read, null);
           
            if(mcursor.getCount()>0)
            {
                while(mcursor.moveToNext())
                {
                    $_update_name = mcursor.getString(mcursor.getColumnIndex("name"));
                    $_update_emailid = mcursor.getString(mcursor.getColumnIndex("emailid"));
                    $_update_phoneno = mcursor.getString(mcursor.getColumnIndex("phoneno"));
                    $_update_address = mcursor.getString(mcursor.getColumnIndex("address"));
                }
               
                edit_update_name.setText(""+$_update_name);
                edit_update_emailid.setText(""+$_update_emailid);
                edit_update_phoneno.setText(""+$_update_phoneno);
                edit_update_address.setText(""+$_update_address);
               
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Invalid User Name and password ", Toast.LENGTH_SHORT).show();
            }
           
            break;
        case R.id.btn_update_save:
           
           
            $_update_name = edit_update_name.getText().toString();
            $_update_emailid = edit_update_password.getText().toString();
            $_update_phoneno = edit_update_phoneno.getText().toString();
            $_update_address = edit_update_address.getText().toString();
           
           
           
            if($_update_username ==null || $_update_password ==null || $_update_name ==null || $_update_emailid ==null || $_update_phoneno ==null ||  $_update_address.equals("") || $_update_username.equals("") || $_update_password.equals("") || $_update_name.equals("") || $_update_emailid.equals("") || $_update_phoneno.equals("") || $_update_address.equals(""))
            {
                Toast.makeText(getApplicationContext(), "Above the Field is Empty", Toast.LENGTH_SHORT).show();
            }
            else
            {
               
                String sqllite_save = "SELECT * FROM insert_records WHERE username='"+$_update_username+"' AND password='"+$_update_password+"'";
                Cursor mcursor_save = sqlite_db.rawQuery(sqllite_save, null);
               
                if(mcursor_save.getCount()>0)
                {
                    String sqllite_update= "UPDATE insert_records SET name='"+$_update_name+"',emailid='"+$_update_emailid+"',phoneno='"+$_update_phoneno+"',address='"+$_update_address+"' WHERE username='"+$_update_username+"' AND password='"+$_update_password+"'";
                    sqlite_db.execSQL(sqllite_update);
                   
                    edit_update_username.setText("");
                    edit_update_password.setText("");
                    edit_update_name.setText("");
                    edit_update_emailid.setText("");
                    edit_update_phoneno.setText("");
                    edit_update_address.setText("");
                   
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Invalid User Name and password ", Toast.LENGTH_SHORT).show();
                }
               
            }
           
            break;
        case R.id.btn_update_reset:
           
           
            edit_update_username.setText("");
            edit_update_password.setText("");
            edit_update_name.setText("");
            edit_update_emailid.setText("");
            edit_update_phoneno.setText("");
            edit_update_address.setText("");
           
           
            break;
           
        }
    }
   
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.update, menu);
        return true;
    }

   
}


activity_update.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_background"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="UPDATE PAGE"
            android:textSize="20sp"
            android:gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <EditText
                        android:id="@+id/edit_update_username"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Enter the Username" >

                        <requestFocus />
                    </EditText>

                    <EditText
                        android:id="@+id/edit_update_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:ems="10"
                        android:hint="Enter the password"
                         />

                    <Button
                        android:id="@+id/btn_update_validate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Update"
                        android:layout_gravity="right"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.40"
                            android:text="Name" />

                        <EditText
                            android:id="@+id/edit_update_name"
                           android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.60"
                            android:ems="10" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.40"
                            android:text="Email Id" />

                        <EditText
                            android:id="@+id/edit_update_emailid"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.60"
                            android:ems="10" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView4"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.40"
                            android:text="Phone Number" />

                        <EditText
                            android:id="@+id/edit_update_phoneno"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.60"
                            android:ems="10" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            android:id="@+id/textView5"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.40"
                            android:text="Address" />

                        <EditText
                            android:id="@+id/edit_update_address"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.60"
                            android:ems="10" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal" >

                        <Button
                            android:id="@+id/btn_update_reset"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                             android:layout_gravity="left"
                            android:text="Reset" />

                        <Button
                            android:id="@+id/btn_update_save"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="right"
                            android:text="Update Value" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</LinearLayout>

                      
                                 




DeleteActivity.java


package com.example.crudapps;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DeleteActivity extends Activity implements OnClickListener {

   
    EditText edit_delete_username,edit_delete_password;
    Button btn_delete_deletevalue;
   
   
    String $_delete_username,$_delete_password;
   
    SQLiteDatabase sqlite_db = null;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
       
       
        sqlite_db = openOrCreateDatabase("crud.db", MODE_PRIVATE, null);
        String sql_insert = "CREATE TABLE IF NOT EXISTS insert_records(_id integer primary key autoincrement,name varchar(120),username varchar(120),password varchar(120),emailid varchar(120),phoneno varchar(120),address varchar(120))";
        sqlite_db.execSQL(sql_insert);
       
       
        edit_delete_username = (EditText)findViewById(R.id.edit_delete_username);
       
        edit_delete_password = (EditText)findViewById(R.id.edit_delete_password);
       
        btn_delete_deletevalue = (Button)findViewById(R.id.btn_delete_deletevalue);
        btn_delete_deletevalue.setOnClickListener(this);

    }
   
    @Override
    public void onClick(View v)
    {
   
        switch(v.getId())
        {
        case R.id.btn_delete_deletevalue:
           
            $_delete_username = edit_delete_username.getText().toString();
            $_delete_password = edit_delete_password.getText().toString();
           
            String sqllite_save = "SELECT * FROM insert_records WHERE username='"+$_delete_username+"' AND password='"+$_delete_password+"'";
            Cursor mcursor_save = sqlite_db.rawQuery(sqllite_save, null);
           
            if(mcursor_save.getCount()>0)
            {
                String sqllite_update= "DELETE FROM insert_records WHERE username='"+$_delete_username+"' AND password='"+$_delete_password+"'";
                sqlite_db.execSQL(sqllite_update);
               
                Toast.makeText(getApplicationContext(), "Successfully Record is Deleted", Toast.LENGTH_SHORT).show();
               
            }
            else
            {
                Toast.makeText(getApplicationContext(), "There is No Record in this username and password ", Toast.LENGTH_SHORT).show();
            }
           
           
            break;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.delete, menu);
        return true;
    }
}


activity_delete.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_background"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DELETE PAGE"
            android:textSize="20sp"
            android:layout_gravity="center"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/edit_delete_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" >
                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/edit_delete_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPassword" />

            <Button
                android:id="@+id/btn_delete_deletevalue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="DELETE" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>




Source code:

Download for full source code




No comments:

Post a Comment