Ranjeet singh
Ranjeet singh

Reputation: 794

Update with multiple values in WHERE

I need to update the field in database when it match the two conditions below is my code
this is the query which is working fine for one value in WHERE condition

query2 = "UPDATE  Game_mygame 
          SET game_played = "+str(set_game_played)+" 
          WHERE home_teamID = "+str(i.home_teamID) 

but I need to match two values in WHERE and I am trying the following

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+" 
              WHERE home_teamID = "+str(i.home_teamID)+ 
                    "AND away_teamID = "+str(i.away_teamID)  

but this one gives the syntax error

Please suggest me where I am doing mistake

Upvotes: 2

Views: 848

Answers (2)

Himanshu
Himanshu

Reputation: 32612

I think there is no space between first WHERE condition and AND. So it will make your query like this WHERE home_teamID=xyzAND instead of WHERE home_teamID=xyz AND. So you need to give an extra space before AND (same as you have given it before WHERE)

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              "AND away_teamID = "+str(i.away_teamID)
           ---^^--- Space is missing here

So try this:

query2 = "UPDATE  Game_mygame 
              SET game_played = "+str(set_game_played)+
              " WHERE home_teamID = "+str(i.home_teamID)+ 
              " AND away_teamID = "+str(i.away_teamID)

Upvotes: 2

GautamD31
GautamD31

Reputation: 28753

Concat with . like

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = '". str(i.home_teamID) ."'
          AND away_teamID = '".str(i.away_teamID)."'";  

And also as @Preet Sangha said we can use ids without quotes like

query2 = "UPDATE  Game_mygame 
          SET game_played = ".str(set_game_played)." 
          WHERE home_teamID = ". str(i.home_teamID) ."
          AND away_teamID = ".str(i.away_teamID);  

Upvotes: 1

Related Questions