Python+MySQLでストアドプロシージャの実行(続き)〜複数結果の取得はcursor.nextset()で順次取得
前回「Python+MySQLでストアドプロシージャの実行」で期待通りの結果にならなかった原因が判明。
前回のコード。
#raw_connectionの取得 engine = dbMapper.create_engine(url, echo=False) connection = engine.raw_connection() # Query実行 cursor = connection.cursor() cursor.callproc("ストアドプロシージャ名", [startDate]) results = list(cursor.fetchall()) cursor.close() connection.commit()
てっきり、「cursor.fetchall()」で全ての結果を取得できると思っておりました。
それが間違い。
cursor.nextset()で次の結果に移動しなければならないですね。はい。。。
#raw_connectionの取得 engine = dbMapper.create_engine(url, echo=False) connection = engine.raw_connection() # Query実行 cursor = connection.cursor() cursor.callproc("ストアドプロシージャ名", [startDate]) # 実行結果取得1 result1 = list(cursor.fetchall()) # 次のcursorへ移動 cursor.nextset() # 実行結果取得2 result2 = list(cursor.fetchall()) cursor.close() connection.commit()
こんな感じで結果を取得出来たわけだが、次のように書いて謎のフリーズ。
#raw_connectionの取得 engine = dbMapper.create_engine(url, echo=False) connection = engine.raw_connection() # Query実行 cursor = connection.cursor() cursor.callproc("ストアドプロシージャ名", [startDate]) while cursor.nextset(): result = list(cursor.fetchall()) cursor.close() connection.commit()
原因不明だが、whileの中に入ろうとするとフリーズする・・・。