# -*- coding:utf-8 -*-

import pymysql

user = input('请输入用户名:')
pwd = input('请输入密码:')

# 1.连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123', db='t1', charset='utf8')
print(conn)

# 2.创建游标
cursor = conn.cursor()

#注意%s需要加引号
sql = "select * from t1.userinfo where username='%s' and pwd='%s'" %(user, pwd)
print(sql)

# 3.执行sql语句
cursor.execute(sql)

result=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
print(result)

# 关闭连接,游标和连接都要关闭
cursor.close()
conn.close()

if result:
    print('登陆成功')
else:
    print('登录失败')

下面是执行过程

请输入用户名:lisi
请输入密码:123
<pymysql.connections.Connection object at 0x000001BEFABFE240>
select * from t1.userinfo where username='lisi' and pwd='123'
1
登陆成功
View Code

相关文章:

  • 2022-01-05
  • 2021-06-19
  • 2021-11-21
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-03-03
  • 2021-11-01
猜你喜欢
  • 2021-08-18
  • 2021-11-04
  • 2022-12-23
  • 2021-11-09
  • 2021-05-26
  • 2022-12-23
  • 2021-09-17
相关资源
相似解决方案