SQL语法错误
我想检查是否已经存在用户名。如果是这样,将显示一个消息框,如果不是用户名和字段条目的其余部分将存储在数据库中。但是,当我尝试此功能时,它打印数据库无法连接。这是GUI问题还是数据库问题?
请显示代码和反映错误的完整错误跟踪。 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") 输出:
无法连接到数据库
无法连接到数据库
页:
[1]