mardi 11 août 2015

how to add data in android sqlite

i want to add my new values in the previous one at a sepecific id, but every time when i want to add it created data on a new id rather than added with the previous one.here is the method of my databasehelper class

     public boolean addalldata(String value1, String value2, String value3) {

    String  previous1 = null,previous2 = null,previous3= null;
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    String myquery = "SELECT * FROM student_table WHERE id = 1";
    Cursor res = db.rawQuery(myquery, null);

    while(res.moveToNext()) {
         previous1 = res.getString(1);
         previous2 = res.getString(2);
         previous3 = res.getString(3);
    }


    contentValues.put(COL_2, value1 + previous1);
    contentValues.put(COL_3, value2 + previous2);
    contentValues.put(COL_4, value3 + previous3);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;}

here is the method of mainactivity class

        private void AddAllData() {
          btnaddall.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted =       myDb.addalldata(edit1.getText().toString(),
                            edit2.getText().toString(),
                            edit3.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();


                }
            }
    );

}

Aucun commentaire:

Enregistrer un commentaire