Wednesday, 28 March 2018

SQLite Delete

Hi All,

See the code

mainactivity.java

------------------------------------------------------------------------------------

protected void onCreate(Bundle savedInstanceState) {
            //HUP2();
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main5);
            lstview=(ListView)findViewById(R.id.lst_book);
           
           

           
            int i=0;
            //listitem=new String[5];
           
            db=SQLiteDatabase.openDatabase("data/data/com.example.lms/databases/mydb", null, SQLiteDatabase.OPEN_READONLY);
            String s="select * from tbl_book";
            Cursor c=db.rawQuery(s, null);
            int count=c.getCount();
           
            listitem=new String[count];
            if(c!=null)
            {    
                  if(c.moveToFirst())
                  {
                        do
                        {
                              String book=c.getString(1);

                              listitem[i]=book;
                              i++;
                        }while(c.moveToNext());
                  }
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.lst,R.id.txt_view_book,listitem);
            lstview.setAdapter(adapter);

      lstview.setOnItemClickListener(new OnItemClickListener() {
           
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  // TODO Auto-generated method stub
                  String entry= (String) parent.getAdapter().getItem(position);
                  t1.speak(entry, TextToSpeech.QUEUE_FLUSH, null);
                  //String message = entry.getMessage();


                  //Toast.makeText(getApplicationContext(), entry, Toast.LENGTH_LONG).show();
                  Intent in=new Intent(Main5Activity.this,Main8Activity.class);
                 
                   in.putExtra("txt_view_book",entry);
                   startActivity(in);
            }
      });
           
      }

 ====================================================================

  Main8Activity.java

 ====================================================================

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main8);
            Intent in=getIntent();
            entry=in.getStringExtra("txt_view_book");
            tx2=(EditText) findViewById(R.id.txt_view);
            tx2.setText(entry);
      }
     
      public void deletbook(View view)
      {
           
            //db=SQLiteDatabase.openDatabase("data/data/com.example.lms/databases/mydb", null, SQLiteDatabase.OPEN_READONLY);
            db=this.openOrCreateDatabase("mydb", MODE_PRIVATE, null);
            String s="delete from tbl_book where b_title like '"+tx2.getText()+"'";
            db.execSQL(s);
            Toast.makeText(getApplicationContext(),"Deleted", Toast.LENGTH_LONG).show();
      }

No comments:

Post a Comment