我很熟悉这个错误,因此我将-- create the temporary table
CREATE TABLE #CSVData
(
IdValue int
)
-- create a clustered index for this table (Note: this doesn't need to be unique)
CREATE CLUSTERED INDEX IX_CSVData on #CSVData (IdValue )
-- insert the csv data to the table
BULK INSERT #CSVData
FROM 'c:\csvData.txt'
WITH
(
ROWTERMINATOR = ','
)
-- select the data
SELECT T.*
FROM table_name T
INNER JOIN #CSVData ON(T.uId = IdValue)
-- cleanup (the index will be dropped with the table)
DROP TABLE #CSVData
和interactive_timeout
增加到X秒。因此,每当MySQL空闲超过X秒时,它就会重新连接(根据我的代码)。
以下是代码。
wait_timeout
filename.py
from filename import getconnection, DButil
CONNECTION = getconnection()
def user():
try:
global CONNECTION
if CONNECTION is None:
CONNECTION = getconnection()
except Exception,e:
print e
dbutil = DButil(CONNECTION)
try:
dbutil.data_exist(loginfo)
except Exception,e:
print e
我得到的是连接正在进行并且是打开的,但当我CONNECTION = None
def getconnection(recreate=True, db_name='db_name'):
''' Get a connection, if not available, make it '''
global CONNECTION
if recreate or CONNECTION is None:
if db_name == 'db_name':
CONNECTION = makeconnection()
return CONNECTION
class DButil(object):
'''Low level utiliity arrond db '''
def __init__(self, _connection):
self.connection = _connection
def data_exist(self,data_exist_query):
with self.connection:
print self.connection # <_mysql.connection open to '127.0.0.1' at 1a587b0>
cur = self.connection.cursor()
print cur # <MySQLdb.cursors.Cursor object at 0x1a22710> cur
cur.execute(data_exist_query)
rows = cur.fetchall()
print rows # (2006, 'MySQL server has gone away')
if len(rows) >= 1 :
return True
else :
return False
时,它会抛出rows = cur.fetchall()
。为什么会出现这种行为?
当我重新连接时,即执行(2006, 'MySQL server has gone away')
,它可以正常工作,MySQL连接再次建立。因为,当服务器空闲超过X秒时,我也再次创建了连接,但仍然会抛出错误。为什么?