Python-MySQLdb不能一直正常工作

时间:2017-08-24 09:23:52

标签: python mysql

我有以下代码:

class FootballSQL(object):

   def __init__(self, args=None):
        self.open(args)
        try:
            self.db.begin()
        except:
            pass

    def open(self, args):

        # INITIALIZE MYSQL
        try:
            self.db = MySQLdb.connect(
                    host    = self.__HOST,
                    user    = self.__USER,
                    passwd  = self.__PASSWD,
                    db      = self.__DB,
                    charset = 'utf8mb4'
            );
        except:
            return
        if self.db:
            self.cur = self.db.cursor()

    def rollback(self):
        if self.db:
            self.db.rollback()
            self.cur.close()
            self.db.close()

       def close(self):
        if self.db:
            self.db.commit()
            self.cur.close()
            self.db.close()


if len(cnt) > 0: count = cnt['cnt']
else: count = 0
try:
    # UPDATE MATCH
    fsql.cur.execute("UPDATE `Matches` SET `HostGoal`='%d', `GuestGoal`='%d', `Contributes`='%d', `Active`='0' WHERE `ID`='%d' AND `Active`='2';" % (
            self.host_goal,
            self.guest_goal,
            count,
            self.id
    ))

    # "UPDATE `Users` INNER JOIN `Temp` ON `Users`.`ChatID`=`Temp`.`ChatID` INNER JOIN `Matches` ON `Matches`.`ID`=`Temp`.`MatchID` SET `Users`.`FirstScore`=`Users`.`FirstScore`+8, `Users`.`SecondScore`=`Users`.`SecondScore`+8, `Users`.`Step`='[28, 8]' WHERE `Temp`.`MatchID`='28' AND `Temp`.`HostGoal`-`Temp`.`GuestGoal`=`Matches`.`HostGoal`-`Matches`.`GuestGoal` AND ((`Temp`.`HostGoal`>`Temp`.`GuestGoal` AND `Matches`.`HostGoal`>`Matches`.`GuestGoal`) OR (`Temp`.`HostGoal`<`Temp`.`GuestGoal` AND `Matches`.`HostGoal`<`Matches`.`GuestGoal`) OR (`Temp`.`HostGoal`=`Temp`.`GuestGoal` AND `Matches`.`HostGoal`=`Matches`.`GuestGoal`));"
    # 10 - GOALS
    fsql.cur.execute("UPDATE `Users` INNER JOIN `Temp` ON `Users`.`ChatID`=`Temp`.`ChatID` INNER JOIN `Matches` ON `Matches`.`ID`=`Temp`.`MatchID` SET `Users`.`FirstScore`=`Users`.`FirstScore`+10, `Users`.`SecondScore`=`Users`.`SecondScore`+10, `Users`.`Step`='[%d, 10]', `Temp`.`Step`=-1 WHERE `Temp`.`MatchID`='%d' AND `Temp`.`Step`=0 AND `Temp`.`HostGoal`=`Matches`.`HostGoal` AND `Temp`.`GuestGoal`=`Matches`.`GuestGoal`;" % (self.id,self.id))

    # 8 - GOALS
    fsql.cur.execute("UPDATE `Users` INNER JOIN `Temp` ON `Users`.`ChatID`=`Temp`.`ChatID` INNER JOIN `Matches` ON `Matches`.`ID`=`Temp`.`MatchID` SET `Users`.`FirstScore`=`Users`.`FirstScore`+8, `Users`.`SecondScore`=`Users`.`SecondScore`+8, `Users`.`Step`='[%d, 8]', `Temp`.`Step`=-1 WHERE `Temp`.`MatchID`='%d' AND `Temp`.`Step`=0 AND `Temp`.`HostGoal`-`Temp`.`GuestGoal`=`Matches`.`HostGoal`-`Matches`.`GuestGoal`;" % (self.id,self.id))

    # 5 - GOALS
    fsql.cur.execute("UPDATE `Users` INNER JOIN `Temp` ON `Users`.`ChatID`=`Temp`.`ChatID` INNER JOIN `Matches` ON `Matches`.`ID`=`Temp`.`MatchID` SET `Users`.`FirstScore`=`Users`.`FirstScore`+5, `Users`.`SecondScore`=`Users`.`SecondScore`+5, `Users`.`Step`='[%d, 5]', `Temp`.`Step`=-1 WHERE `Temp`.`MatchID`='%d' AND `Temp`.`Step`=0 AND ((`Temp`.`HostGoal`-`Temp`.`GuestGoal` > 0 AND `Matches`.`HostGoal`-`Matches`.`GuestGoal` > 0) OR (`Temp`.`HostGoal`-`Temp`.`GuestGoal` < 0 AND `Matches`.`HostGoal`-`Matches`.`GuestGoal` < 0));" % (self.id,self.id))

    # 2 - GOALS
    fsql.cur.execute("UPDATE `Users` INNER JOIN `Temp` ON `Users`.`ChatID`=`Temp`.`ChatID` INNER JOIN `Matches` ON `Matches`.`ID`=`Temp`.`MatchID` SET `Users`.`FirstScore`=`Users`.`FirstScore`+2, `Users`.`SecondScore`=`Users`.`SecondScore`+2, `Users`.`Step`='[%d, 2]', `Temp`.`Step`=-1 WHERE `Temp`.`MatchID`='%d' AND `Temp`.`Step`=0;" % (self.id,self.id))

    # COMMIT
    fsql.close()
except:
    fsql.rollback()
    reset_command(text="Try again")
    return

它在大多数情况下都很有效,但有时只执行一些查询而后来的查询不起作用。例如# 10 - GOALS查询有效,但# 8 - GOALS和...没有,并且没有例外投掷 我真的不知道为什么,但这让我发疯了! 我也用这段代码测试:

for i in range(1000):
    fsql.cur.execute("UPDATE Temp set GuestGoal=GuestGoal+1;")

但是这很好用,所以我认为问题不在于快速执行 那么,我做错了什么?

1 个答案:

答案 0 :(得分:1)

问题不是来自查询或任何事情,只是我的类的值有时会在其他用户的查询之间被替换。