1

I am Inserting and Updating the table By using below code

int rowsaffected = db.update(MyDatabaseHelperDon.TABLE_CURRENT, values,MyDatabaseHelperDon.VARIABLE_CURRENT + " = " + "\"" + var + "\"",null);

if(rowsaffected == 0) {
        db.insert(MyDatabaseHelperDon.TABLE_CURRENT, null,values);
}

where MyDatabaseHelperDon.TABLE_CURRENT is my table name, 'values' is my Content Values, MyDatabaseHelperDon.VARIABLE_CURRENT is column name, 'var' is the Variable present in the Column.

This Code is working Fine. Now For this i need to add If Condition like 'if(value != 400)' if it satisfies then only it should update or insert.

Please Guide me How to achieve this.

  • You can check before calling the insert/update function. Please post the code of calling insert function. – Aniruddha Jul 17 '14 at 04:02
  • db.update will return integer value, if value is updated it will return 1 else 0, if it is zero i am Inserting the Value. Insert Function is written, Please check the code – user3705553 Jul 17 '14 at 04:06

1 Answers1

0

If I understood your question correctly, you can try something like this:

private void insertUpdate ()
{
  // Your Insert / Update code here...
     int rowsaffected = db.update(MyDatabaseHelperDon.TABLE_CURRENT, values,MyDatabaseHelperDon.VARIABLE_CURRENT + " = " + "\"" + var + "\"",null);

     if(rowsaffected == 0) 
     {
        db.insert(MyDatabaseHelperDon.TABLE_CURRENT, null,values);
     }
}

public void callingMethod()
{
    int value = 0;// Initialize here 
    value =    // process this value further based on your logic....
    if (value !=400)
    {
        insertUpdate ();
    }
    else
   {
        // when value is 400
   }

}
Aniruddha
  • 4,457
  • 1
  • 20
  • 38
Nishanthi Grashia
  • 9,787
  • 5
  • 42
  • 57