pymysql如何登录
时间: 2023-10-27 17:57:18 浏览: 87
要使用pymysql登录,你需要按照以下步骤进行操作:
1. 导入pymysql库,并创建数据库连接和游标。示例代码如下:
```
import pymysql
conn = pymysql.connect(host='localhost', user='root', password='123456', database='db_game')
cursor = conn.cursor(pymysql.cursors.DictCursor)
```
2. 新建一个user_dao文件,处理登录业务逻辑。示例代码如下:
```
import user_mysql
import user_model
def login_dao(name, password):
if len(user_mysql.get_by_name(name)) > 0:
res = user_mysql.get_by_name(name)[0]
user = user_model.User(i_d=res['id'], name=res['name'], password=res['password'])
if user.name == name and user.password == password:
return 1
else:
return '密码错误'
return '用户名不存在'
```
3. 编写SQL语句。由于用户名是唯一的,所以只需要检索用户名对应的那一条信息,然后将密码进行对比,如果一样则可以成功登录。示例代码如下:
```
sql = '''SELECT * FROM tbl_user WHERE username=%s'''
```
以上是使用pymysql进行登录的步骤和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [使用Pymysql进行用户登录验证](https://blog.csdn.net/jhdddd/article/details/121228773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* [pymysql 连接 MySQL 实现简单登录!](https://blog.csdn.net/weixin_43881394/article/details/109132040)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)