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,477
  • 2
  • 21
  • 39
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • value here is the column name, if 400 is not present in column value in table then i need to update – user3705553 Jul 17 '14 at 04:09
  • @user3705553: you can use a where condition for Update. Or,You can have a select query before calling the insert update method . – ngrashia Jul 17 '14 at 04:11
  • May be this link can help you for update with condition : http://stackoverflow.com/q/9453651/3492139 – ngrashia Jul 17 '14 at 04:13
  • if i use where condition for checking value condition it will return 0 if value is equal to 400, and insert query will insert 400 again :( – user3705553 Jul 17 '14 at 04:13
  • So thinking of writing Insert and Update on Same Query. Can u Please Help How to write this – user3705553 Jul 17 '14 at 04:14
  • For insert and update in same query refer : and . – ngrashia Jul 17 '14 at 04:16
  • @ Nishanthi Grashia Thank u for your Valuable Time :) Will try and Implement as in above Links – user3705553 Jul 17 '14 at 04:18
  • @user3705553: Please check the grey tick mark next to this answer to accept as answer if it helps! Also, when you have enough reputation, try upvoting answers that helped you! – ngrashia Jul 17 '14 at 04:19