jeudi 31 mars 2016

In Android studio how to delete row using sqllite

I wrote a code to delete a row. It is not deleting a row. It is showing error in toasr. Suppose value is abc no such column: abc(code 1):, while compliling: Delete from FACULTY where NAME = abc; is comming. What should i do? java file is given below.

package com.adu.collegemanagementsystem;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DeleteFaculty extends AppCompatActivity    {
EditText id,name;
Button submit;
//MyDBHandler dbHandler;
SQLiteDatabase db;
ContentValues cv;
Cursor c;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_delete_faculty);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    id = (EditText) findViewById(R.id.id);
    name = (EditText) findViewById(R.id.name);
    submit= (Button) findViewById(R.id.add);

    try {
         db=openOrCreateDatabase("CMS",MODE_PRIVATE,null);


          db.execSQL("create table IF NOT EXISTS FACULTY(FID INTEGER PRIMARY      KEY AUTOINCREMENT,"
                + "NAME varchar(50) NOT NULL,"
                + "ADDRESS varchar(50),"
                + "QUALIFICATION varchar(50) ,"
                + "SALARY varchar(50) NOT NULL,"
                + "USERNAME varchar(50) ,"
                + "PASSWORD varchar(100))");

        Toast toast = Toast.makeText(DeleteFaculty.this, "Database Created   Sucessfull ",Toast.LENGTH_LONG);
        toast.show();
    }
    catch (Exception e){

         Toast toast = Toast.makeText(DeleteFaculty.this, "DB not created.",Toast.LENGTH_SHORT);
        toast.show();

    }


}

public void SubmitClicked(View view){

        String fname= name.getText().toString();
        if(fname.isEmpty()){
            Toast toast = Toast.makeText(DeleteFaculty.this, "Please Enter Faculty Name.",Toast.LENGTH_SHORT);
            toast.show();
            name.setFocusable(true);
            name.requestFocus();
        }

        else
        {
            cv=new ContentValues();
            cv.put("NAME", fname);
            try {
                db.execSQL("Delete from FACULTY where NAME = "+fname+";");
              // db.delete("FACULTY", "NAME = " + fname, null);
                Toast toast = Toast.makeText(DeleteFaculty.this, " Faculty Deleted Successfully  .",Toast.LENGTH_SHORT);
                toast.show();


            }

             catch (Exception e){
                Toast.makeText(DeleteFaculty.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();

             }




        }
    }
  }

Aucun commentaire:

Enregistrer un commentaire