2

当它匹配以下两个条件时,我需要更新数据库中的字段是我的代码
,这是对于WHERE条件中的一个值可以正常工作的查询

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

但我需要匹配两个值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)  

但是这个给出了语法错误

请建议我在哪里做错了

4

2 回答 2

2

我认为第一个WHERE条件和AND. 所以它会让你的查询像这样WHERE home_teamID=xyzAND而不是WHERE home_teamID=xyz AND. 所以你需要在之前给一个额外的空间AND(和你之前给的一样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

所以试试这个:

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)
于 2013-08-12T05:12:46.593 回答
1

.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)."'";  

正如@Preet Sangha所说,我们可以使用不带引号的 id

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);  
于 2013-08-12T05:07:43.907 回答