solution for python django.db.utils.InterfaceError: (0, '')

created at 07-15-2021 views: 3

error message

django.db.utils.InterfaceError: (0, '')

Reasons for database disconnection

Because the default wait_timeout=28800 of the database, the unit is second (s), it is 8 hours when converted to hours (it is related to the mysql setting information). That is, once my service is started, it will be automatically disconnected by mysql after 8 hours.

Solution

This is caused by the global cursor. Try to create and close the cursor within each method, which requires the original query.

Code

cursor = connection.cursor()
cursor.execute(query)
cursor.close()

Or use with:

with connections.cursor() as cursor:
    cursor.execute(sql)
Please log in to leave a comment.