skip to Main Content

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


  1. Here’s a few thing you could check on and improve:

    Timeout: MySQL’s wait_timeout might be closing inactive connections.

    • Fix: Increase wait_timeout or periodically run a SELECT 1 query to keep the connection alive.

    Query Formatting: Instead of:

    query = ("SELECT * FROM search WHERE time= %s;", (date,))
    

    Use:

    query = "SELECT * FROM search WHERE time= %s"
    cursor.execute(query, (date,))
    

    Reconnect: Set autocommit to True and use conn.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.

    Login or Signup to reply.
  2. 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 a tuple or a list with the actual values to replace the %s placeholders:

    cursor.execute("SELECT * FROM search WHERE time = %s", (date,))
    

    The second argument is a tuple in this case. But you had the equivalent of:

    cursor.execute(("SELECT * FROM search WHERE time= %s;", (date,)))
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search