|
发表于 2021-5-22 01:40:48
|
显示全部楼层
&
- 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")
复制代码 |
|