案例:用户登陆 pyMySQL数据库版本

 

用户登陆 pyMySQL数据库版本

 1 # __author:"Cheng"
 2 # date:2018-12-06
 3 
 4 import pymysql
 5 
 6 # 获取用户输入
 7 your_name = input('请输入您的账号:')
 8 your_pwd = input('请输入您的密码:')
 9 
10 # 连接数据库
11 conn = pymysql.connect(host="localhost", user="root", password="123456", database="usrinfo", charset="utf8")
12 # 得到一个可以执行SQL语句的光标对象
13 cursor = conn.cursor()
14 # 查询数据的SQL语句
15 sql = "select * from info where usr_name=%s and usr_pwd=%s;"
16 # 获取所查询条数
17 ret = cursor.execute(sql, [your_name, your_pwd])  # 让pyMySQL帮我们拼接数据,防止SQL注入
18 
19 if ret:                         # 如果有内容
20     print("登陆成功!")
21 else:                           # 否则
22     print("用户名或密码错误。")
23 
24 cursor.close()
25 conn.close()

 

posted @ 2018-12-06 14:44  宝露  阅读(320)  评论(0)    收藏  举报