LOGIN SYSTEM #3: Checking for repeating Username

Making the register system is one thing, but how do we make sure that two people do not use the same username. There are two things we need to consider while doing this: 

  • SQL is case insensitive, so as far as SQL is concerned, ABC and abc are the same.
  • However, for a username, case sensitivity must be taken into account.
We can tackle this by doing a two step process. First, we get usernames from SQL without case sensitivity and then we can use case sensitive Python to differentiate case. 

For example: If given username is ABC and the SQL database has abc and ABc, we can use SQL execute statement to get abc and ABc and then we can use Python to differentiate between ABC and ABc or abc. 

The code goes like this:








So first, we search the SQL database for the username and get any rows with that username (case not taken into account) and store it in data. If the length of data is 0, then there is no row, so we do not need to do any check and can directly add the username and password to the database. But, if len(data) is not 0, then we need to check for case. So, we use Python's equality operator to do the check as it returns True only if case is also equal. Now, let's check how it works:

Case 1: Username exists:- 




Case 2:- Username doesn't exist:-




Here, though the text by-itself is the same, the case is different and therefore, we can take it as a new username. Let's check the database to see if it is updated.








It is. Now, what's left for us to do is create a way for existing users to login. Login System #4 will be coming shortly. Until then, check out the other posts:








Comments