使用时出现InterfaceError(0,“”)adbapi连接

2024-10-05 12:17:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用twisted.enterpriseadbapi提供的数据库连接池来实现并行数据库写入,如下所示

dbpool = adbapi.ConnectionPool('pymysql', **config)

query = dbpool.runInteraction(insert, item)
query.addErrback(handler_error)

def insert(cursor, item):
    cursor.execute('SELECT id from area where name = %s', (item['area'],))
    area_id = cursor.fetchone()[0]
    cursor.execute('SELECT id from location where name = %s', (item['location'],))
    location_id = cursor.fetchone()[0]
    insert_sql = 'INSERT INTO `community` (`community_id`,`name`, `area_id`, `location_id`) VALUES (%s, %s, %s, %s)'
    params = (item['community_id'], item['name'], area_id, location_id)
    cursor.execute(insert_sql, params)

def handler_error(self, failure):
    print('--------------database operation exception!!-----------------')
    print(failure)

我知道adbapi connectionPool为每个方法/线程提供一个连接,这意味着每个insert都有一个db连接。但是,insert方法中有三个sql,它们共享相同的连接和游标。你知道吗

我搜索了google,发现InterfaceError (0, '')问题可能是因为在sql执行之前关闭了游标。你知道吗

在我的情况下,这是因为我在同一个方法中有三个sql导致了InterfaceError (0, '')异常。有什么办法处理吗?你知道吗


Tags: 方法namecommunityid数据库executesqlarea

热门问题