2016-04-01 14 views
1

Ich spiele mit großen Daten in Python und MySQL.Python MySQL Connector

Ich habe eine riesige Tabelle, ich muss neue Zeilen einfügen, während ich Abfrageergebnisse abrufe.

Ich habe diesen Fehler habe:

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 

Der Code ist die folgende:

#!/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() 

ich ich weiß, eine gepufferte Verbindung zu mysql verwenden könnte, aber es ist keine gute Wahl in mein Fall wegen, wie wirklich groß mein Tisch ist!

Antwort

3

Dies ist ein dokumentiertes Verhalten von cursor.fetchmany():

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

Um dieses Problem zu überwinden, können Sie eine neue Verbindung herstellen, indem write_cursor verwendet werden:

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

Ich hatte ein ähnliches Problem, wenn ich war threading, und musste nur eine neue Verbindung in jedem Thread separat schließen und öffnen ... anstatt die Verbindung offen zu lassen.

Verwandte Themen