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

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()

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x