Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Pymysql for user login authentication

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use Pymysql for user login authentication". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The specific implementation ideas and processes are as follows:

1. First, create a table of user-related information in the database, mainly user name and password. Where id is the primary key and the user name is unique.

2. Import the pymysql library and create database connections and cursors.

Import pymysql

Conn = pymysql.connect (

Host='localhost'

User='root'

Password='123456'

Database='db_game'

)

Cursor = conn.cursor (pymysql.cursors.DictCursor)

3. Write sql. Since the user name is unique here, I only need to retrieve the information corresponding to the user name, then compare the password, and I can log in successfully.

Sql ='

SELECT * FROM tbl_user WHERE username=%s

''

4. Get the user name and password entered by the user.

Username = input ('Please enter user name:')

Password = input ('Please enter password:')

5. Execute the sql statement to get the information in the database table. Because the user name is unique, there must be only one piece of information, so fetchone () is used to get the information.

Cursor.execute (sql, username)

A = cursor.fetchone ()

6. Make judgments. If the returned result set is empty, the user name entered does not exist in the data table. If it exists, compare the password. If the comparison is not successful, it is a password error; if the comparison is successful, you can log in successfully.

If an is None:

Print ("user does not exist!")

Elif a ['password']! = password:

Print ("wrong password!")

Else:

Print ("login successfully!")

Finally, don't forget to close cursors and databases.

Cursor.close ()

That's all for conn.close () "how to use Pymysql for user login authentication". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report