vendredi 25 septembre 2015

How to update and delete data from table in MainActivity in SQLITEOPENHELPER

I am Created a database for students with fields Name,surname,marks and I did insert and view Sucesfully but I don't know how to do Update and Delete please help. here i want to do update and delete based on ID I did an Update coading in DAbaseclas but when i going to declare in MainActivity i got an error here i attach an image When i declare update code in mainActivity i got error like this

This is wht i want to perform My databasehandler class

public class DatabaseHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME="students.db";
private static final String TABLE_NAME="student_table";
private static final String COL_1="ID";
private static final String COL_2="NAME";
private static final String COL_3="SURNAME";
private static final String COL_4="MARKS";
private static final int DATABASE_VERSION=1;


public DatabaseHandler(Context context) {
    super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL( "Create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
    Log.d("oncreate","Table Was Created"); }@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
    onCreate(db);

}
public boolean insertdata(String name,String surname,String marks)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues= new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,surname);
    contentValues.put(COL_4,marks);
    long result=db.insert(TABLE_NAME,null,contentValues);
    if (result==-1)
        return false;
    else
        return true;
}
public Cursor getalldata(){
    SQLiteDatabase db=this.getReadableDatabase();
   Cursor res=db.rawQuery("SELECT * FROM " +TABLE_NAME,null);
    return res;

}public int Update(int id,String Name,String surname,string marks){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues= new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);return db.update(TABLENAME, contentValues,"ID=?",new String[] {id});}

MainActivity

enter code herepublic class MainActivity extends ActionBarActivity {
DatabaseHandler myDB;
EditText editname,editsurname,editmarks,updatename,updatesurname,updatemark;
Button bsave,bview,bupdate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
myDB= new DatabaseHandler(this);

    editname= (EditText) findViewById(R.id.editText);
    editsurname= (EditText) findViewById(R.id.editText2);
    editmarks= (EditText) findViewById(R.id.editText3);
    updatename= (EditText) findViewById(R.id.updatename);
    updatemark= (EditText) findViewById(R.id.updatemark);
    updatesurname= (EditText) findViewById(R.id.updatesurname);
    bsave= (Button) findViewById(R.id.button);
    bview= (Button) findViewById(R.id.button2);
    bupdate= (Button) findViewById(R.id.button4);

    AddData();
    viewall();

}
public void AddData()
{
 bsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isInserted=myDB.insertdata(editname.getText().toString(),editsurname.getText().toString(),editmarks.getText().toString());
            if (isInserted=true)
                Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
            else
               Toast.makeText(MainActivity.this,"Data Not Inserted",Toast.LENGTH_LONG).show(); }
    });
}
public void viewall()
{
    bview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Cursor res=myDB.getalldata();
            if (res.getCount()==0)
            {
                ShowMessage("Error","Nothing Found");
                return;
            }
            StringBuffer buffer= new StringBuffer();
            while (res.moveToNext()){
                buffer.append("Id :" +res.getString(0)+"\n");
                buffer.append("Name :" +res.getString(1)+"\n");
                buffer.append("Surname :" +res.getString(2)+"\n");
                buffer.append("Marks :" +res.getString(3)+"\n");
          }
           ShowMessage("Data",buffer.toString());
        }
    });

}
public void ShowMessage(String title,String message)
{
    AlertDialog.Builder builder= new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

Aucun commentaire:

Enregistrer un commentaire