Reputation: 103
I have the following code:
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
It works great most of the time, But sometimes just some of queries execute and later ones doesn't work. For example # 10 - GOALS
query works, but # 8 - GOALS
and ... doesn't, And no exception throwing.
I really don't know why, But it's making me crazy!
Also I test with this code:
for i in range(1000):
fsql.cur.execute("UPDATE Temp set GuestGoal=GuestGoal+1;")
But this works fine, So I think the problem is not with executing quickly.
So, What I'm doing wrong?
Upvotes: 1
Views: 40
Reputation: 103
The problem wasn't from query or anything, Just my class's values sometime replaced in between of queries by other users.
Upvotes: 1