'Always getting this kinda error... 'mysql.connector.errors.InternalError: Unread result found'

" I'm working on a little project which connnects MYSQL and PYTHON. If I succeed in this then one can use the whole MYSQL server via PYTHON! ** But the thing is that I get raised by mysql.connector errors!"

while True:

DB = input("enter the database:")
str1 = "show databases"
C.execute(str1)
for i in C:
    if DB in i:
        print("Selected", DB)
        db = "use {}".format(DB)
        C.execute(DB)
        break
    else:
        continue
else:
    print("Unknown Database")
    continue
break

output: enter image description here



Solution 1:[1]

You are re-using the cursor before the all the results from the execution of "SHOW DATABASES" has been consumed. The simplest solution is to fetch all the results and then iterate over them.

str1 = "show databases"
C.execute(str1)
# Fetch all the names into a list.
database_names = C.fetchall()
for i in database_names:
    if DB in i:
        print("Selected", DB)
        db = "use {}".format(DB)
        C.execute(DB)
        break
    ...

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 snakecharmerb