我正在使用 Python 和 MySQL 处理大数据。
我有一个巨大的表,我需要在获取查询结果时插入新行。
我有这个错误:
Traceback (most recent call last):
File "recsys.py", line 53, in <module>
write_cursor.executemany(add_sim, data_sims)
File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 603, in executemany
self._connection.handle_unread_result()
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 1057, in handle_unread_result
raise errors.InternalError("Unread result found")
mysql.connector.errors.InternalError: Unread result found
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import mysql.connector
import numpy
import scipy
from scipy.spatial import distance
logging.basicConfig(filename='recsys.log', level=logging.INFO)
cnx = mysql.connector.connect(user='...', password='...', database='...')
cursor = cnx.cursor(dictionary=True)
write_cursor = cnx.cursor()
query = ("...")
cursor.execute(query)
while True:
rows = cursor.fetchmany(100)
if not rows:
break
add_sim = ("...")
data_sims = []
for row in rows:
f1 = row['f1']
f2 = row['f2']
v1 = [[row['a1'], row['b1'], row['c1']]]
v2 = [[row['a2'], row['b2'], row['c2']]]
c = 1 - scipy.spatial.distance.cdist(v1, v2, 'cosine')
if c > 0.8:
data_sim = (f1, f2, c)
data_sims.append(data_sim)
write_cursor.executemany(add_sim, data_sims)
cnx.commit()
cursor.close()
cnx.close()
我知道我可以使用到 mysql 的缓冲连接,但在我的情况下这不是一个好的选择,因为我的表有多大!