I have a trouble on running my query by using mysqlclient on Python. Here is what I did:
- Connect to DB
- Query looping through dict
Here is what I got when I hit index 16:
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
It is run on Linux but not on my Windows environment.
What is this error ?
Here is my code and position of the error query
campaignsListReportData = {}
#Connect to Database
db = MySQLdb.connect('mysqlserver', 'user', 'password', 'selecteddb')
cursor = db.cursor()
query = """
SELECT
some column
FROM
some table
WHERE
some condition;
"""
cursor.execute(query)
data = cursor.fetchall()
for row in data:
campaignReport = {}
connectionState = False
while connectionState is False:
try:
url = "a URL"
r = requests.get(url, verify=False)
feedback = r.json()
if int(feedback['success']) is 0:
connectionState = True
elif int(feedback['success']) is 2:
connectionState = True
else:
cursor.execute("SELECT TRIM(SUM(counter)) FROM d WHERE a = " + str(row[1]))
counterURL = cursor.fetchone()
counterURLNumber = 0
if counterURL[0] == None:
counterURLNumber = 0
else:
counterURLNumber = counterURL[0]
urlData = []
cursor.execute("""SELECT long_url, counter , TRIM((counter / (SELECT SUM(counter) FROM d WHERE a = """ + str(row[1]) + """) * 100)) AS 'Percentage' """
"""FROM d """
"""WHERE c = """ + str(row[1]))
urlQuery = cursor.fetchall()
for row in urlQuery:
urlInfo = {
'label': row[0],
'value': row[1],
'percentage': row[2]
}
urlData.append(urlInfo)
campaignsListReportData[str(row[0])] = campaignReport
connectionState = True
except requests.exceptions.RequestException as e:
print(str(datetime.now() - timedelta(minutes=60)))
print("Connection Failed, Trying again . . .")
db.close()
Error occurred on:
cursor.execute("SELECT TRIM(SUM(counter)) FROM d WHERE a = " + str(row[1]))
Thanks