'"mysql.connector.errors.InternalError: Unread result found" is being produced when entering book code

Output:

Welcome back  Manas . What do you want to do? 

1.Add book to borrow
2.Return book borrowed
3.Check existing borrowed book(s)
4.Logout
Enter your choice: 1
Enter code of book to be borrowedAA111

Results in:

Traceback (most recent call last): File "C:\Users\Rotten\Desktop\test.txt", line 53, in userlist(username,bookcode,bookborrowdate) File "C:\Users\Rotten\Desktop\test.txt", line 22, in userlist cursor.execute("insert into userlist values(%s,%s);",(username,bookdict,)) File "C:\Users\Rotten\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\cursor_cext.py", line 243, in execute self._cnx.handle_unread_result() File "C:\Users\Rotten\AppData\Local\Programs\Python\Python310\lib\site-packages\mysql\connector\connection_cext.py", line 794, in handle_unread_result raise errors.InternalError("Unread result found") mysql.connector.errors.InternalError: Unread result found

Here is the minimal code sample

import mysql.connector
import hashlib
import os
from datetime import date
mycon=mysql.connector.connect(host="localhost",user="root",passwd="123456",database="library",charset="utf8mb4")
cursor=mycon.cursor()
stmt = "SHOW TABLES LIKE 'users'"
cursor.execute(stmt)
result = cursor.fetchone()
if result:
    pass
else:
    cursor.execute("create table userlist(username varchar(20),book varchar(200));")
def userlist(username,bookcode,bookborrowdate):
    bookdict={}
    booktick=cursor.execute("Select book from userlist where username=%s",(username,))
    if booktick is not None:
        bookdict=booktick
    else:
        pass
    bookdict.update({'bookcode':'bookborrowdate'})
    cursor.execute("insert into userlist values(%s,%s);",(username,bookdict,))
    mycon.commit()
def userfull(username):
    cursor.execute("select book from userlist where username=%s;",(username,))
    bookdata=cursor.fetchone()
    if bookdata is None:
        print("No books")
    else:
        bookneed=bookdata[0]
        for key, value in bookneed.items():
            print(key, ' : ', value)
def userret(username,bookcode):
    cursor.execute("select book from userlist where username=%s;",(username,))
    bookreturn=cursor.fetchone()
    if bookreturn is None:
        print("No books borrowed")
    else:
        bookretex=bookreturn[0]
        if bookcode in bookretex:
            del bookretex[bookcode]
username="Manas"
print("Welcome back ",username,". What do you want to do? \n")
print("1.Add book to borrow")
print("2.Return book borrowed")
print("3.Check existing borrowed book(s)")
print("4.Logout")
usch=int(input("Enter your choice: "))
if usch==1:
    while True:
        bookcode=input("Enter code of book to be borrowed")
        bookborrowdate=date.today()
        userlist(username,bookcode,bookborrowdate)
        td=timedelta(10)
        print("Please return this book by",bookborrowdate+td)
        choice=input("Continue?[y/n]")
        if choice in 'Yy':
            continue
        else:
            break
elif usch==2:
    while True:
        bookcode=input("Enter code of borrowed book to be returned")
        returnticket=userret(username,bookcode)
        returncookie=input("continue? [y/n]: ")
        if returncookie in "Yy":
            continue
        else:
            break
elif usch==3:
    userfull()
elif usch==4:
    print("Logging out of ",username,"...")
    username=None
else:
    print("Wrong input, try again")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source