(一)pymysql模块的下载:pip3 install pymysql

# 实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中)


import pymysql
user = input('请输入用户名:')

pwd = input('请输入密码:')



# 1.连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', db='db8', charset='utf8')
#host,port,user,password,db,charset这些参数是必须的。


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

#注意%s需要加引号
sql = "select * from 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('登录失败')
在python中的代码显示

相关文章:

  • 2021-05-11
  • 2021-05-28
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2022-02-28
  • 2022-12-23
  • 2021-10-15
  • 2021-09-04
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案