0

I have a databaseHelper class. But, I want to multiply two values of each row and store it in another column while a button is clicked. I have checked my query and it works fine but I want to directly multiply the values and make change in the database when a button is click. I don't want to return any values but simply execute the query.

What I did for this is I have created a method in my database helper class. This is the method:

public void getTicketTotal(){
    SQLiteDatabase db=this.getWritableDatabase();
    db.rawQuery("update ticket_table set total=quantity*item_price ",null);
}

and in the button click event I did this. But, it didn't multiply and store the values in the table:

 myDb=new DatabaseHelper(TicketActivity.this);

                myDb.getTicketTotal();

What to do?

Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32

2 Answers2

1

The problem was with the method that I was using. The solution is to use db.execSQL instead of rawQuery. So the line of code would be:

public void getTicketTotal(){
    SQLiteDatabase db=this.getWritableDatabase();
     db.execSQL("update ticket_table set total=quantity*item_price ");
}

and simply call it from within the activity.

Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32
0

your update query is wrong. on which row you have to update total?

hope it helps

public void getTicketTotal(){
      SQLiteDatabase db=this.getWritableDatabase();
      db.rawQuery("update ticket_table tt set total= tt.quantity * tt.item_price" ,null);
    }
Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26