Dom
Dom

Reputation: 61

sqlite android update with where and when condition

I have an app where I can add some lessons (English,French...). Each lesson has a lot of informations, so as the theme of the lesson.

Let's say I create this :

Lesson : English Theme : Reading

Lesson: French Theme : Reading

Now I want to update the theme of my lesson French (to Grammar). But the problem is that I was updating both of the lessons. Then I tried something like this to just update Reading to Grammar:

        db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DESIGNATIONCOURS+"='"+newdesignation+"' where "+COLONNE_DESIGNATIONCOURS +"='"+olddesignation+"' when "+COLONNE_BRANCHECOURS+"='"+branche+"'");

TABLE_COURS is my db table.

COLONNE_DESIGNATIONCOURS is where I save the theme of the lessons

COLONNE_BRANCHECOURS is where I save the name of the lesson.

Then all the values I putted are correct, I have already checked it.

The problem is that, if I have the same theme in different lessons, at the beginning it was updating all the values where the theme was Reading, so English was affected as well, but I want to only update French's theme

I have a try catch in my app, everything works well until that update. Can you tell me the error of my query?

Upvotes: 0

Views: 22

Answers (1)

Dom
Dom

Reputation: 61

I figured it out alone !

My query was wrongly written. I changed my WHEN to AND and it worked !

My new query is this one :

        db.execSQL("UPDATE "+TABLE_COURS+" set "+COLONNE_DESIGNATIONCOURS+"='"+newdesignation+"' where "+COLONNE_DESIGNATIONCOURS +"='"+olddesignation+"' and ("+COLONNE_BRANCHECOURS+"='"+branche+"')");

Upvotes: 0

Related Questions