1

I want to add my new values in the previous one at a specific 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();


                }
            }
    );

}
1
  • 2
    insert creates a new record. you want update instead. Commented Aug 11, 2015 at 17:12

2 Answers 2

2

try this:

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

        String  previous1 = "0",previous2 = "0",previous3= "0";
        long result = 0;
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        String myquery = "SELECT * FROM student_table WHERE id = 1";
        Cursor res = db.rawQuery(myquery, null);
        if(res.getCount()>0){
             res.moveToFirst();
             previous1 = res.getString(1);
             previous2 = res.getString(2);
             previous3 = res.getString(3);

             try{
               contentValues.put(COL_2, Integer.parseInt(value1) + Integer.parseInt(previous1));
               contentValues.put(COL_3, Integer.parseInt(value2) + Integer.parseInt(previous2));
               contentValues.put(COL_4, Integer.parseInt(value3) + Integer.parseInt(previous3));
               result = db.update(TABLE_NAME, contentValues, "id = ?", new String[]{"1"});
             }catch(Exception e){
               result = -1;
             }


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

}

if there is no record in the database then insert a new record using db.insert otherwise update the existing record by using db.update

Sign up to request clarification or add additional context in comments.

8 Comments

after pasting ur code , in my databasehelper class, it does nt any happen , record 1 not add the new value in the previous one
may be the id of the record is not 1
now its just concatenate the new value with the previous one but not added both , i want addition and they are concatenate
just give me example how do you want to concatenate them
for example if previous value was 4 , and new value was 3 then result should be 7 not 34
|
0

try res.moveToFirst().
This will move your cursor to first position so that furthur checking starts from the first row.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.