InterfaceError (0, '')

I have built a site using Django and I am receiving this annoying error when I am trying to execute a query.

If I restart the Apache server, the error will go away for a short time.

Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/fran/cron/views/set_caches.py" in set_caches
24.         cursor.execute(query, [category['id']])
File "/usr/local/lib/python2.7/site-packages/django/db/backends/util.py" in execute
15.             return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
86.             return self.cursor.execute(query, args)
File "build/bdist.linux-i686/egg/MySQLdb/cursors.py" in execute
155.         charset = db.character_set_name()

Exception Type: InterfaceError at /blablabla/
Exception Value: (0, '')

This is caused by a global cursor. Try creating and closing the cursor within each method a raw query is needed.

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

You get this error when you have a db.close() call and later try to access the database without creating a new connection. Try to find if you close the connection to the database when you don't mean to.


I agreed with Moberg. This error is caused when we try to access the database after we have closed the connection. This could be caused by some wrong indentation in the code. Below is my code.

conn = connect()
cur = conn.cursor()
tk = get_tickers(cur)
for t in tk:
    prices = read_price(t, cur)
    if prices != None:
        update_price(t, cur)
        print 'Price after update of ticker ', t, ':'
        p_open, p_high, p_low, p_close = read_price(t, cur)
        print p_open, p_high, p_low, p_close
    else:
        print 'Price for ', t, ' is not available'
    conn.close()

I got the same error as reported by Marian. After dedenting conn.close(), everything worked well. Confirmed that global conn is not an issue.