InterfaceError(0,'')

Mar*_*ian 51 python mysql django

我使用Django构建了一个站点,当我尝试执行查询时,我收到了这个恼人的错误.

如果我重新启动Apache服务器,错误将在短时间内消失.

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, '')
Run Code Online (Sandbox Code Playgroud)

scu*_*cum 61

这是由全局游标引起的.尝试在需要原始查询的每个方法中创建和关闭游标.

cursor = connection.cursor()
cursor.execute(query)
cursor.close()
Run Code Online (Sandbox Code Playgroud)

  • 全局游标有什么问题?我遇到了这个问题,并试图弄清楚发生了什么. (2认同)

Mob*_*erg 41

当您有一个db.close()电话,然后尝试访问数据库而不创建新连接时,您会收到此错误.尝试查找您是否关闭与数据库的连接.

  • 这可以通过检查:`if(not db_handle.open):db_handle = MySQLdb.connect(**login_data)来完成. (6认同)
  • 也会在超时后发生,而不仅仅是在主动 close() 之后 (2认同)

小智 5

我同意莫伯格的观点。当我们在关闭连接后尝试访问数据库时会导致此错误。这可能是由代码中的某些错误缩进引起的。下面是我的代码。

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()
Run Code Online (Sandbox Code Playgroud)

我得到了与玛丽安报告的相同的错误。在 dedenting conn.close() 之后,一切运行良好。确认全局连接不是问题。