如何防止用户输入与其他用户相同的用户名或用户 ID?
How can I prevent a user inputting the same username or userID as another user?
我制作了一个注册系统,它接收用户信息并将其存储在数据库中。我已将 userID 和用户名设为主键,如何更改我的代码以使用户无法使用相同的用户名或 userID 注册。
def register_user():
userid_info = userid.get()
username_info = username.get()
password_info = password.get()
name_info = name.get()
phonenumber_info = phonenumber.get()
email_info = email.get()
region_info = region.get()
accesslevel_info = accesslevel.get()
# Sql code for writing the data that was written in the regsitering page.
cursor = cnn.cursor()
query = "INSERT INTO `users`(`userID`, `userName`, `userPassword`, `name`, `phoneNum`, `email`, `region`, `accessLevel`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
query_vals = (userid_info, username_info, name_info, password_info, phonenumber_info, email_info, region_info, accesslevel_info)
cursor.execute(query, query_vals)
cnn.commit()
cursor.close()
cnn.close()
# removes the values in the entrys once the user selects that the registration was successful
userid_entry.delete(0, END)
username_entry.delete(0, END)
password_entry.delete(0, END)
name_entry.delete(0, END)
phonenumber_entry.delete(0, END)
email_entry.delete(0, END)
region_entry.delete(0, END)
accesslevel_entry.delete(0, END)
Label(screen1, text = "Registration successful", fg = "green", font = ("calibri", 11)).pack()
如果有相同用户名的用户,您可以在新注册发生时在您的数据库中搜索。只需 select 来自您的数据库,其中用户名等于新注册用户名。
您可以创建一个列表,其中存储所有 ID 和密码,程序会在每次有人输入新 ID 时检查列表中是否存在一致的名称
由于您已经将用户 ID 和用户名作为主键,因此您可以使用异常来实现您的逻辑来警告用户。
编辑:请按照另一个答案的建议在您的数据库关系中只使用一个主键。
try:
cursor.execute(query)
except IntegrityError:
# handle your exception here
return
或者使用MySQLdb错误
try:
cursor.execute(query)
except MySQLdb.Error as e:
# handle your exception here
return
您应该使用 with
来处理数据库事务,with
处理连接关闭等,即使发生异常也是如此。
我会更改数据库,使主键只是 userID
,然后使 userName
列成为唯一键。这样,您既不能有重复的 userId
列,也不能有重复的 userName
列。如您所见,只有 userId-userName
组合必须是唯一的。然后修改您的代码以处理可能的重复键异常(例如:IntegrityError: (1062, "Duplicate entry '1' for key 'PRIMARY'")) cursor.execute
语句。如果确实遇到异常,如果异常消息中不清楚,则可以查询数据库以确定重复的列。
您必须首先确保您当前的 table 具有唯一的 userId
和 userName
列。例如,
select count(distinct userId), count(distinct userName), count(*) from users;
这三个数字应该都是一样的。如果没有,您可以找到有问题的条目,例如:
select count(userId) as cnt, userId
group by userId
having cnt > 1;
我制作了一个注册系统,它接收用户信息并将其存储在数据库中。我已将 userID 和用户名设为主键,如何更改我的代码以使用户无法使用相同的用户名或 userID 注册。
def register_user():
userid_info = userid.get()
username_info = username.get()
password_info = password.get()
name_info = name.get()
phonenumber_info = phonenumber.get()
email_info = email.get()
region_info = region.get()
accesslevel_info = accesslevel.get()
# Sql code for writing the data that was written in the regsitering page.
cursor = cnn.cursor()
query = "INSERT INTO `users`(`userID`, `userName`, `userPassword`, `name`, `phoneNum`, `email`, `region`, `accessLevel`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
query_vals = (userid_info, username_info, name_info, password_info, phonenumber_info, email_info, region_info, accesslevel_info)
cursor.execute(query, query_vals)
cnn.commit()
cursor.close()
cnn.close()
# removes the values in the entrys once the user selects that the registration was successful
userid_entry.delete(0, END)
username_entry.delete(0, END)
password_entry.delete(0, END)
name_entry.delete(0, END)
phonenumber_entry.delete(0, END)
email_entry.delete(0, END)
region_entry.delete(0, END)
accesslevel_entry.delete(0, END)
Label(screen1, text = "Registration successful", fg = "green", font = ("calibri", 11)).pack()
如果有相同用户名的用户,您可以在新注册发生时在您的数据库中搜索。只需 select 来自您的数据库,其中用户名等于新注册用户名。
您可以创建一个列表,其中存储所有 ID 和密码,程序会在每次有人输入新 ID 时检查列表中是否存在一致的名称
由于您已经将用户 ID 和用户名作为主键,因此您可以使用异常来实现您的逻辑来警告用户。
编辑:请按照另一个答案的建议在您的数据库关系中只使用一个主键。
try:
cursor.execute(query)
except IntegrityError:
# handle your exception here
return
或者使用MySQLdb错误
try:
cursor.execute(query)
except MySQLdb.Error as e:
# handle your exception here
return
您应该使用 with
来处理数据库事务,with
处理连接关闭等,即使发生异常也是如此。
我会更改数据库,使主键只是 userID
,然后使 userName
列成为唯一键。这样,您既不能有重复的 userId
列,也不能有重复的 userName
列。如您所见,只有 userId-userName
组合必须是唯一的。然后修改您的代码以处理可能的重复键异常(例如:IntegrityError: (1062, "Duplicate entry '1' for key 'PRIMARY'")) cursor.execute
语句。如果确实遇到异常,如果异常消息中不清楚,则可以查询数据库以确定重复的列。
您必须首先确保您当前的 table 具有唯一的 userId
和 userName
列。例如,
select count(distinct userId), count(distinct userName), count(*) from users;
这三个数字应该都是一样的。如果没有,您可以找到有问题的条目,例如:
select count(userId) as cnt, userId
group by userId
having cnt > 1;