2016-05-27 60 views
0

我正在使用Python,我試圖從同一個數據庫(name = begin)中的兩個mySQL表(name = deal_info和profile)中檢索數據。這兩個表沒有聯繫或者不具有相同的主鍵,但我得到的錯誤從兩個MySQL表中檢索Python中的數據?

raise errors.InternalError("Unread result found.") 
mysql.connector.errors.InternalError: Unread result found. 

下面是代碼

import mysql.connector 
from decimal import * 

cnx = mysql.connector.connect(user='root', password = 'xxx', database='begin') 
cursor = cnx.cursor() 

query_deal_info = ("SELECT mrp, qty_remain, deal_price, qty, asp FROM deal_info WHERE deal_id = %s") 
deal_id = int(input("Enter the deal id:")) 
cursor.execute(query_deal_info,(deal_id,)) 
query_profile = ("SELECT user_id, abp1, abp2, abp3, abpf FROM profile WHERE user_id = %s") 
user_id = int(input("Enter the user id:")) 
cursor.execute(query_profile,(user_id,)) 

for (mrp, qty_remain, deal_price, qty, asp) in cursor: 
print("The MRP for the deal is {}".format(mrp)) 
print ("The deal price is {}, asp is {} and qty is {}".format(deal_price, asp, qty)) 

for (user_id, abp1) in cursor: 
    print("The ABP1, 2, 3 and f for the deal is {}, {}".format(user_id, abp1)) 

cursor.close() 
cnx.close() 

回答

0

每MySQL.connector docs,你需要獲取結果

In this case, you must be sure to fetch all rows of the result set before executing any other statements on the same connection, or an InternalError (Unread result found) exception will be raised.

所以,簡單地cursor.execute()後運行循環打印線:

執行任何其他語句之前
cnx = mysql.connector.connect(user='root', password = 'xxx', database='begin') 
cursor = cnx.cursor() 

# DEAL INFO 
query_deal_info = "SELECT mrp, qty_remain, deal_price, qty, asp \ 
        FROM deal_info WHERE deal_id = %s" 
deal_id = int(input("Enter the deal id:")) 
cursor.execute(query_deal_info,(deal_id,)) 

for (mrp, qty_remain, deal_price, qty, asp) in cursor: 
    print("The MRP for the deal is {}".format(mrp)) 
    print ("The deal price is {}, asp is {} and qty is {}".format(deal_price, asp, qty)) 

# PROFILE 
query_profile = "SELECT user_id, abp1, abp2, abp3, abpf \ 
        FROM profile WHERE user_id = %s" 
user_id = int(input("Enter the user id:")) 
cursor.execute(query_profile,(user_id,)) 

for (user_id, abp1) in cursor: 
    print("The ABP1, 2, 3 and f for the deal is {}, {}".format(user_id, abp1)) 

cursor.close()  
cnx.close() 
+0

感謝您的幫助。欣賞它。我嘗試了你的建議,但仍然得到相同的錯誤。但是,當我嘗試只執行一個(交易信息或配置文件),我能夠成功地檢索它。我想從兩個表中檢索的原因是我想比較兩個表中的值(如果deal_price> abp1)。 –

+0

這是我收到的錯誤。 Traceback(最近調用最後一次): 文件「C:/Users/raghu/Desktop/a.py」,第19行,在 profilecursor.execute(query_profile,(user_id,)) 文件「C:\ Python34 \ lib \ site-packages \ mysql \ connector \ cursor.py「,第485行,執行 self._connection.handle_unread_result() 文件」C:\ Python34 \ lib \ site-packages \ mysql \ connector \ connection.py「 ,第1057行,在handle_unread_result 引發errors.InternalError(「未讀結果」) mysql.connector.errors.InternalError:未發現結果 –

+0

我修改了我早先的答案。不需要使用不同名稱的遊標。根據文檔,您需要在執行另一個查詢語句之前獲取。對於'pymysql',這不是必需的。 – Parfait

相關問題