0

I have a trouble on running my query by using mysqlclient on Python. Here is what I did:

  1. Connect to DB
  2. 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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adityo Setyonugroho
  • 877
  • 1
  • 11
  • 29

1 Answers1

0

try with cursor.execute("SELECT TRIM(SUM(counter)) FROM d WHERE a = %s", (str(row[]))) the built in function execute has 2 parameters, the first is the query to execute and the second %s statement, so we can pass multiple values to that selection. Remember we can have multiple %s in one statement and then pass the value in the second parameter. Read more What does %s mean in Python?

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27