raise errors.InterfaceError(ERR_NO_RESULT_TO_FETCH) mysql.connector.errors.InterfaceError: No result set to fetch from

Viewed 515

I'm trying to make a simple tkinter program where there's entries to put your username, email, and password and I am trying to make it check if there a username already exists and it gives me this error. I know there has been other questions about it on stackoverflow but those did not help me out.


import mysql.connector
from tkinter import *

root = Tk()
root.title("Login")
root.iconbitmap("profile.ico")

db = mysql.connector.connect(
    db = "pythonlogin",
    host="127.0.0.1",
    user="root",
    password="[password]"
)

cursor = db.cursor()

usernameLabel = Label(root, text='Username: ')
emailLabel = Label(root, text='Email: ')
passwordLabel = Label(root, text='Password: ')

usernameEntry = Entry(root, width=30)
emailEntry = Entry(root, width=30)
passwordEntry = Entry(root, width=30)

usernameLabel.grid(row=0, column=0)
emailLabel.grid(row=1, column=0)
passwordLabel.grid(row=2, column=0)

usernameLabel.config(font=("Roboto", 12))
emailLabel.config(font=("Roboto", 12))
passwordLabel.config(font=("Roboto", 12))

usernameEntry.grid(row=0, column=1)
emailEntry.grid(row=1, column=1)
passwordEntry.grid(row=2, column=1)

class userInfo():
    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.password = password
    
    def addInfoToDB(self):
        query = "INSERT INTO pylogin (Username, Email, Password) VALUES (%s, %s, %s)"
        values = (self.username, self.email, self.password)
        cursor.execute(query, values)
        db.commit()
        print(cursor.rowcount, "record inserted.")

errorLabel = Label(root)
errorLabel.grid(row=4, column=1)

def submitInfo():
    fetchInfo = cursor.fetchall()
    if usernameEntry.get() == "" or emailEntry.get() == "" or passwordEntry.get() == "":
        errorLabel.config(text="Error: Couldn't get value of all entries", fg="red")
    if (usernameEntry.get(),) in fetchInfo:
        errorLabel.config(text="Username already exists in the database!")
    else:
        errorLabel.config(text="Success!", fg="green")
        details = userInfo(usernameEntry.get(), emailEntry.get(), passwordEntry.get())
        details.addInfoToDB()

submitButton = Button(root, text="Submit", command=submitInfo)
submitButton.grid(row=3, column=1, ipadx=50)
submitButton.config(font=("Roboto", 10))

root.mainloop()

1 Answers

In the submitInfo method, you have fetchInfo = cursor.fetchall() but no query has been executed.

>>> import mysql.connector as mc
>>> conn = mc.connect(database='test')
>>> cur = conn.cursor()
>>> cur.fetchall()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
...
mysql.connector.errors.InterfaceError: No result set to fetch from

You need to execute a SELECT query before you can fetch a result.

It looks as if you are checking whether the username entered exists in the database, so you probably want to query the database for that name:

def submitInfo():

    if usernameEntry.get() == "" or emailEntry.get() == "" or passwordEntry.get() == "":
        errorLabel.config(text="Error: Couldn't get value of all entries", fg="red")

    # Query the database for a row with a matching username
    stmt = """SELECT Username, Email, Password FROM pylogin WHERE Username = %s""" 
    cursor.execute(stmt, (usernameEntry.get(),))
    # There should be at most one matching row, so we can use
    # the cursor's fetchone method to retrieve it.
    row = cursor.fetchone()
    if row:
        errorLabel.config(text="Username already exists in the database!")

    else:
        errorLabel.config(text="Success!", fg="green")
        details = userInfo(usernameEntry.get(), emailEntry.get(), passwordEntry.get())
        details.addInfoToDB()
Related