I a unsure why my connection is giving me an error while I loop through my table to search for elements of my list:
try:
conn = mysql.connector.connect(user='root', password='####',host='####',database='###', allow_local_infile=True)
cursor = conn.cursor()
except Exception:
traceback.print_exc()
print("I am unable to connect to the database")
index= 0
for i in range(0, len(listdates)):
date= listdates[i]
print(date)
try:
query = ("SELECT * FROM search WHERE time= %s;", (date,))
cursor.execute(query)
row= cursor.fetchall()
if not row:
index = i
break
except Exception:
traceback.print_exc()
print("did not return row")
I am not sure what the issue is. My connection is fine since when I execute the query outside the loop I have no issue. In the larger code this will go in, I have no issue looping through my queries. I don’t know what is wrong.
error:
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at ‘127.0.0.1:3306’, system error: Connection not available.
I attempted to use cursor.close() in the loop. I also have tried using "
with conn.cursor() as cursor:" outside of of the loop.
2
Answers
Here’s a few thing you could check on and improve:
Timeout: MySQL’s
wait_timeout
might be closing inactive connections.wait_timeout
or periodically run aSELECT 1
query to keep the connection alive.Query Formatting: Instead of:
Use:
Reconnect: Set
autocommit
toTrue
and useconn.ping(reconnect=True)
to auto-reconnect.Server Logs: Check MySQL server logs for errors or restarts.
Network: Ensure a stable network connection if MySQL is remote.
Resource Limits: Ensure MySQL isn’t hitting max connections.
Manage Cursors: Close cursors after use, but no need to close/reopen for each loop iteration.
Context Managers: Use
with conn.cursor() as cursor:
outside the loop for better resource management.Remember to isolate the issue by testing with smaller data or adding logs to pinpoint the exact problem.
The
execute
method takes one or two arguments, two arguments being required when the first argument, your SQL statement as a string, has%s
placeholders. In this case the second argument should be atuple
or alist
with the actual values to replace the%s
placeholders:The second argument is a tuple in this case. But you had the equivalent of:
I have taken the liberty of replacing variable
query
with its actual value in the above method call. You are passing a single argument that is a tuple. This is what is causing your problem.