1 # 实现:使用Python实现用户登录,如果用户存在则登录成功(假设该用户已在数据库中) 2 3 4 import pymysql 5 # user = input('请输入用户名:') 6 # 7 # pwd = input('请输入密码:') 8 9 10 11 # 1.连接 12 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', db='sxl', charset='utf8') 13 14 15 # 2.创建游标 16 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 17 18 #注意%s需要加引号 有sql注入问题li' or 1=1 -- zdxv 或lijie' -- zdxv 19 # sql = "select * from userinfo where name='%s' and pwd='%s'" %(user, pwd) #注意修改内容及表 20 # print(sql) 21 # #解决sql注入问题 22 # sql="select * from userinfo where name=%s and pwd=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上 23 # result=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。 24 25 # 3.执行sql语句 26 # cursor.execute(sql) 27 # result=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目 28 # print(result) 29 30 31 # #1.增加数据 32 # sql = "insert into userinfo(name,pwd) values (%s,%s)" 33 # # effect_row = cursor.execute(sql,(123,123)) 34 # #同时插入多条数据 35 # # effect_row = cursor.executemany(sql,[('李四','110'),('王五','119')]) 36 # # print(effect_row) 37 # # 2.改 38 # sql = "update userinfo set name = %s where id = 4" 39 # effect_row = cursor.execute(sql,'111') 40 # print(effect_row) 41 # # 3.删 42 # sql = "delete from userinfo where id >13" 43 # effect_row = cursor.execute(sql) 44 # print(effect_row) 45 # 4.查 46 sql = 'select * from userinfo' 47 num = cursor.execute(sql) #11 48 # # 4.1fetchone():获取下一行数据,第一次为首行; 49 # #查询第一行的数据 50 # row = cursor.fetchone() 51 # print(row) # (1, 'lijie', '123') 52 # # 查询第二行数据 53 # row = cursor.fetchone() 54 # print(row) # (3, 'zz', '123') 55 # 4.2fetchall():获取所有的数据 56 # rows = cursor.fetchall() 57 # print(rows) #((1, 'lijie', '123'), (3, 'zz', '123'), (5, '王五', '119'), (6, '123', '123')) 58 #注1:在实例化的时候,将属性cursor设置为pymysql.cursors.DictCursor,每一行的数据都会生成一个字典 59 #[{'id': 1, 'name': 'lijie', 'pwd': '123'}, {'id': 3, 'name': 'zz', 'pwd': '123'}, {'id': 5, 'name': '王五', 'pwd': '119'}, {'id': 6, 'name': '123', 'pwd': '123'}] 60 # #注2:在fetchone示例中,在获取行数据的时候,可以理解开始的时候,有一个行指针指着第一行的上方,获取一行,它就向下移动一行,所以当行指针到最后一行的时候,就不能再获取到行的内容,所以我们可以使用如下方法来移动行指针: 61 # cursor.scroll(1,mode='relative') # 相对当前位置移动 62 # cursor.scroll(3,mode='absolute') # 相对绝对位置移动 以第一行为参考 63 # # 第一个值为移动的行数,整数为向下移动,负数为向上移动,mode指定了是相对当前位置移动,还是相对于首行移动 64 row = cursor.fetchone() 65 print(row) # {'id': 1, 'name': 'lijie', 'pwd': '123'} 原始数据是135678 66 cursor.scroll(1,mode='relative') 67 row = cursor.fetchone() 68 print(row) # {'id': 5, 'name': '王五', 'pwd': '119'} 69 cursor.scroll(0,mode='absolute') #设置之后,光标相对于首行没有任何变化,所以打印的结果为第一行数据 70 row = cursor.fetchone() 71 print(row) # {'id': 1, 'name': 'lijie', 'pwd': '123'} 72 cursor.scroll(1,mode='absolute') 73 row = cursor.fetchone() 74 print(row) # {'id': 3, 'name': 'zz', 'pwd': '123'} 75 cursor.scroll(-1,mode='relative') #设置之后,光标相对于当前位置往前移动了一行 76 row = cursor.fetchone() 77 print(row) # {'id': 3, 'name': 'zz', 'pwd': '123'} 78 #4.3 fetchmany(4):获取4行数据 79 # row = cursor.fetchmany(2) 80 # print(row) # [{'id': 1, 'name': 'lijie', 'pwd': '123'}, {'id': 3, 'name': 'zz', 'pwd': '123'}] 81 82 83 84 #在数据库里增、删、改的时候,一定记得commit 85 conn.commit() 86 87 88 89 90 91 # 关闭连接,游标和连接都要关闭 92 cursor.close() 93 conn.close() 94 95 # if result: 96 # print('登陆成功') 97 # else: 98 # print('登录失败')