diff --git a/db.py b/db.py
index 105c8dcd24946759ea4cae043e14aef111a26267..356da83b63d93e167f184573dab74054b0fec27d 100644
--- a/db.py
+++ b/db.py
@@ -67,15 +67,7 @@ elif config['DB_ENGINE'] == 'mysql':
 		else:
 			db = mysql.connector.connect(user=config['MYSQL_USER'], password=config['MYSQL_PASSWD'], unix_socket=config.get('MYSQL_UNIX', None))
 		cur = db.cursor()
-		tries = 0
-		while (tries < 10):
-			try:
-				cur.execute(operation)
-			except mysql.connector.errors.InternalError as e:
-				if e.msg == 'Deadlock found when trying to get lock; try restarting transaction':
-					tries += 1
-				else:
-					raise
+		cur.execute(operation)
 		rows = []
 		try:
 			rows = cur.fetchall()
@@ -95,7 +87,17 @@ elif config['DB_ENGINE'] == 'mysql':
 def query(operation, *params, delim="sep", nlfix=True):
 	operation, params = fix_query(operation, params)
 	cur = get_dbcursor()
-	cur.execute(operation, params)
+	tries = 0
+	while (tries < 10):
+		try:
+			cur.execute(operation, params)
+		except mysql.connector.errors.InternalError as e:
+			if e.msg == 'Deadlock found when trying to get lock; try restarting transaction':
+				tries += 1
+				continue
+			else:
+				raise
+		break
 	rows = []
 	try:
 		rows = cur.fetchall()