In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the pymysql implementation of user login authentication code how to write the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this pymysql to achieve user login authentication code how to write the article will have a harvest, let's take a look.
Thinking process
Until then, I have been wrestling with a problem. The user name is unique, but the password is repeated, a SQL statement, how to choose between fetchone and fetchall. As a result, the brain is constantly cut, the management is also messed up, and the whole thing is intertwined. After thinking calmly, I found the problem, why can there be only one SQL statement in the program? Can't the two contents be separated?
As a result, there was an inspiration. First use a SQL statement to search for the username field in the data table because the user name is unique and the result set of the search uses fetchone. Determine whether the result set is empty, if not, you can judge whether the password is consistent, and if you are consistent, you will log in successfully, and inconsistency means that the password is wrong. If the result set is empty, the next SQL statement is executed.
This SQL statement searches the data table for all the information in the password field that is the same as the password. In this case, you use fetchall to get everything, and the result is a list. If the list is empty, the user name and password are incorrect; if the list is not empty, the user name is entered incorrectly.
After some testing and verification, the effect is not bad! The following is the specific code content.
Specific code import pymysql# creates a database connection conn = pymysql.connect (host='localhost', user='root', password='123456' Database='db_game') # get the user name and password entered by the user username= input ("Please enter the user name:") password = input ("Please enter the password:) # create a cursor cursor = conn.cursor (pymysql.cursors.DictCursor) # query the same information as the user name entered in the database sql =''SELECT * FROM tbl_user WHERE username=%s'''# execute sql language cursor.execute (sql)" Username) # because the user name is unique So use fetchone to get a = cursor.fetchone () # if the result set obtained is empty if an is None:## query database all the same information as the password entered by the user sql =''SELECT * FROM tbl_user WHERE password=%s'''cursor.execute (sql, [password]) # # password is repeatable, so use fetchalll = cursor.fetchall () # # to judge if the result set list is empty Indicates that there is no such user. If l = []: print ("user name and password entered incorrectly!") # # otherwise, the user name is entered incorrectly. Else: print ("user name entered wrong!") # the result set obtained is not empty. Else:## determines whether the value of the password field in the result set is the same as the password entered. If consistent, successfully log in to if a ['password'] = = password: print (' successful login!') # # otherwise, the password is entered incorrectly, else: print ('incorrect password!') This is the end of the article cursor.close () conn.close () on "how to write the code for user login authentication in pymysql". Thank you for reading! I believe that everyone has a certain understanding of "pymysql to achieve user login authentication code how to write" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.