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?

6
  • 1
    try using db. execSQL instead of db.rawQuery. It is used when you are expecting some results in return Commented Feb 14, 2017 at 9:48
  • 1
    FYI, You are using UPDATE query then where is WHERE statement ? Commented Feb 14, 2017 at 9:48
  • Why negative vote??? Commented Feb 14, 2017 at 9:48
  • @Gautam ok Gautam. Commented Feb 14, 2017 at 9:49
  • 1
    where clause is not mendatory to update table data. Commented Feb 14, 2017 at 9:55

2 Answers 2

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.

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

3 Comments

good. So annoying. Sorry bro will not got this. Thanks
It's totally ok bro. :) The great part is we actually learned something.
yes bro :) happy to talk with you
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);
    }

15 Comments

But this query works fine while I am separately applying it in my db table.
Why do I need a where statement here?
Because you want to update on single data right? So you need to set WHERE condition. Like "WHERE Id = 3"
I think it helps you. If this helps mark this as a right answer. Thanks Bro
@ZakiPathan where clause is not mandatory.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.