Wednesday, November 18, 2015

ANDROID SQLITE- (CRUD OPERATIONS)

ANDROID SQLITE DATABASE:
        try
        {
      
        db = openOrCreateDatabase("repository",MODE_PRIVATE,null);
        db.execSQL("CREATE TABLE IF NOT EXISTS table1(id int,fname VARCHAR(50),lname VARCHAR(50));");
        Toast.makeText(this,"DATABASE INITIALISED", Toast.LENGTH_LONG).show();
        }
        catch(Exception ex)
        {
        Toast.makeText(this,"ERROR.."+ex.getMessage(), Toast.LENGTH_LONG).show();
        }
GETING MAX VALUE (AGGREGATE FUNCTION IN ANDROID:
    public void Maxval()
    {
       String maxval="";
       try
       {
       String query="select max(id) as maxid from table1";
       Cursor c = db.rawQuery(query,null);
        if (c.moveToFirst()) {
           // temp_address = c.getString(c.getColumnIndex("lastchapter"));
               maxval=c.getString(0);
               maxid=Integer.parseInt(maxval)+1;
               
        }
        showdisplaysmall(maxval);
        c.close();
       }
       catch(Exception ex)
       {
              showdisplaylong(ex.getMessage());
       }
      
    }
INSERT SQLITE QUERY IN ANDROID
  public void insert(View v)
    {
       Maxval();
       EditText fname=(EditText)findViewById(R.id.fname);
       EditText lname=(EditText)findViewById(R.id.lname);
      
       db.execSQL("insert into table1 values("+maxid+",'"+fname.getText().toString()+"','"+lname.getText().toString()+"')");
       Toast.makeText(this, "INSERTED...", Toast.LENGTH_LONG).show();
      
    }
UPDATE AND DELETE QUERY IN SQLITE:
    public void update(View v)
    {
    
       String query="update table1 set fname= '"+fname+"',lname='"+lname+"' where id=";
       AlertDisplay(query,"ENTER ID TO UPDATE","..Updated");
    }
  public void delete(View v)
    {
       String query="delete from table1 where id=";
       AlertDisplay(query,"ENTER ID TO DELETE","..DELETED");
      
    }
ALERT DISPLAY METHOD:
public void AlertDisplay(final String query,String title,final String passMessage)
    {
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setTitle(title);

       // Set up the input
       final EditText input = new EditText(this);
       // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
       input.setInputType(InputType.TYPE_CLASS_NUMBER);
       builder.setView(input);

       // Set up the buttons
       builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
             
               m_Text = input.getText().toString();
               int id=Integer.parseInt(m_Text);
               
               String fullquery=query+id;
               try
               {
               db.execSQL(fullquery);
               }
               catch(Exception ex)
               {
                     showdisplaylong(ex.getMessage());
               }
               showdisplaylong("Record.."+m_Text+passMessage);
           }
           
       });
       builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.cancel();
               showdisplaysmall("Operation Canceled By User");
           }
       });

       builder.show();
      
    }


DISPLAY TOAST METHOD:
   public void showdisplaylong(String val)
    {
       Toast.makeText(this, val, Toast.LENGTH_LONG).show();
      
      
    }


No comments:

Post a Comment