Hi,
I am wanting to check if a username already exists. If so a message box will be displayed, if not the username and the rest of the field entries will be stored in the database. However when I tried this, it prints database can’t connect. I tried this without line 65-69 and it works perfectly.
Is this a GUI question or a database question?
Please show code and a full error traceback that reflects the error.
(May-02-2021, 12:06 PM)Yoriz Wrote: [ -> ]Is this a GUI question or a database question?
Please show code and a full error traceback that reflects the error.
How do I post an image here?
def register(self):
if self.name.get() == "" or self.username.get() == "" or self.password.get() == "":
messagebox.showwarning("Alert", "All fields are required!")
elif len(self.username.get()) < 3 or len(self.username.get()) > 25:
messagebox.showwarning("Alert", "Username must be between 3 and 25 characters!")
elif len(self.password.get()) < 6 or len(self.password.get()) > 25:
messagebox.showwarning("Alert", "Password must be between 6 and 25 characters!")
else:
try:
db = mysql.connector.connect(
host="localhost",
username="dbuser01",
password="computing",
database="geography_quiz"
)
db_cursor = db.cursor()
db_cursor.execute("SELECT * FROM user_information WHERE Username=%s", self.username.get())
row = db_cursor.fetchone()
if row is not None:
messagebox.showerror("Error", "Username already exists!")
else:
db_cursor.execute("INSERT INTO user_information (Full_name, Username, Password) values(%s, %s, %s)",
(self.name.get(),
self.username.get(),
self.password.get()
))
db.commit()
db.close()
messagebox.showinfo("Registration Success!", "Welcome!")
except:
print("Cannot connect to database")
By not checking for a particular exception you have no idea what went wrong
add a raise so you can find out what the exception is
...
...
...
except:
print("Cannot connect to database")
raise
then add that add code to deal with that particular exception or fix the unintended error.
Output:
Cannot connect to database
Cannot connect to database
Thanks
When I tried this it says
No connection could be made because the target machine actively refused it