2016-04-01 18 views
1

私はPythonとMySQLで大きなデータを使って遊んでいます。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へのバッファの接続を使用することができます知っているが、それはでは良い選択ではありません私の場合は、私のテーブルが本当にどのように大きいのために!

答えて

3

これはcursor.fetchmany()の文書化された動作です:

You must fetch all rows for the current query before executing new statements using the same connection.

この問題を克服するために、あなたはwrite_cursorが使用する新しい接続を確立することができます

cnx_write = mysql.connector.connect(...) 
write_cursor = cnx_write.cursor() 
0

をするとき、私、私は同様の問題がありましたスレッディングがあっただけで、接続を開いたままにする代わりに、各スレッドで別々に新しい接続を閉じて開く必要がありました。

関連する問題