diff --git a/db.py b/db.py
index 356da83b63d93e167f184573dab74054b0fec27d..74db0a142d24f84d273bc201a7e3190bd69fa81a 100644
--- a/db.py
+++ b/db.py
@@ -86,10 +86,10 @@ elif config['DB_ENGINE'] == 'mysql':
 
 def query(operation, *params, delim="sep", nlfix=True):
 	operation, params = fix_query(operation, params)
-	cur = get_dbcursor()
 	tries = 0
 	while (tries < 10):
 		try:
+			cur = get_dbcursor()
 			cur.execute(operation, params)
 		except mysql.connector.errors.InternalError as e:
 			if e.msg == 'Deadlock found when trying to get lock; try restarting transaction':