我使用df <- read.table(text = "KEY A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4
1 120 100 NA 110 1 1 NA 1 NA NA NA NA
2 100 NA 115 NA NA NA NA NA Y N Y N",
header = TRUE, stringsAsFactors = FALSE)
adbapi
提供的数据库连接池来实现并行数据库写入如下
twisted.enterprise
我知道adbapi connectionPool为每个方法/线程提供一个连接,这意味着每个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)
都有一个数据库连接。但是,insert
方法中有三个sql,共享相同的连接和游标。
我搜索了谷歌,发现insert
问题可能是因为游标在执行sql之前被关闭了。
所以在我的情况下,是因为我在同一个方法中有三个sql会导致InterfaceError (0, '')
异常。还有处理它的想法吗?