pymysql模块学习路径:增删改的时候一定要conn.commit()
pymysql模块实现登录功能:
1 import pymysql 2 3 user = input("username:") 4 pwd = input("password:") 5 6 conn = pymysql.connect(host="localhost",user='root',password='123456',database="db5") 7 #游标 8 cursor = conn.cursor() 9 #连接数据库成功 10 11 12 #不要自己做字符串拼接,这样是错误的,会造成数据库中没有的账号也会登录成功 13 sql = "select * from userinfo where username=%s and password=%s " 14 #execute会自己帮我们拼接字符串 15 # cursor.execute(sql,{'u':user,'p':pwd}) 16 cursor.execute(sql,[user,pwd]) 17 #取结果的第一条 18 result = cursor.fetchone() 19 20 if result: 21 print("登录成功") 22 else: 23 print("登录失败") 24 25 #关闭数据库 26 cursor.close() 27 conn.close()
运行结果: