两次重用游标时发现 MySQL 未读结果

问题描述

问候

我正在尝试在 Pi 上为智能锁编写 Python 脚本,它可以检测 RFID 并使用 TOTP 对其进行身份验证。当我运行脚本时,它可以很好地检测第一张卡片,并从 #Start#End 完成,但是当我尝试点击第二张卡片时,它失败了 MysqL.connector.errors.InternalError: Unread result found在行 cursor.execute("Select id,name,secret From users Where rfid_uid="+str(id)) 中。

我能做些什么来修复它,这样我就不需要为了检测第二张卡而重新运行脚本。

两个卡的 uid 都在数据库中。

脚本

try:
    while True:
        
        #Start
        
        print("Place card near the scanner")
        mylcd.lcd_display_string("Place card near",1)
        mylcd.lcd_display_string("the scanner",2)
        id,text = reader.read()
        cursor.execute("Select id,secret From users Where rfid_uid="+str(id)) #Traceback said it crash here when Im trying to tap different card (second card)
        result = cursor.fetchone()
        
        if cursor.rowcount >= 1:
            mylcd.lcd_clear()
            print("Welcome,",result[1])
            mylcd.lcd_display_string("Welcome",1)
            mylcd.lcd_display_string(result[1],2)
            time.sleep(2)
            mylcd.lcd_clear()
            print("Type the code from your authenticator")
            mylcd.lcd_display_string("Type in the code",1)
            
            cursor.execute("Select secret From users Where rfid_uid="+str(id))
            secret = result[2]
            

            totp = pyotp.TOTP(str(secret))
            otp = totp.Now()
            
            your_code = input('')
            
            mylcd.lcd_display_string(your_code,2)
            
            if your_code == otp:
                mylcd.lcd_clear()
                print("Code Valid,opening the door")
                mylcd.lcd_display_string("Code Valid",1)
                mylcd.lcd_display_string("opening the door",2)
                time.sleep(5)
            else:
                mylcd.lcd_display_string("Invalid Code",1)
                print("Invalid")
                
            time.sleep(5)
            mylcd.lcd_clear()
            
            #End

except KeyboardInterrupt:
    mylcd.lcd_clear()
    print("\nApplication Stopped")

finally:
    GPIO.cleanup()

谢谢!

解决方法

如果我正确理解问题,我最近遇到了同样的问题。要使游标能够多次执行,您可以向游标添加 buffered 属性并将其设置为 true。你可以这样添加:


cursorVariable = database.cursor(buffered = True)

希望这能解决您的问题并祝您好运!