kuwoyinlehe 2020-03-01
就是利用mysql 语法 使其 查询条件永远为真
解决方案 让mysql帮我们拼接
import pymysql conn = pymysql.connect(host='127.0.0.1', user='root', password="123", database='day43') cur = conn.cursor() user = "akhksh' or 1=1 ;-- " password = '*******' sql = "select * from userinfo where username = %s and password =%s;" print(sql) cur.execute(sql,(user,password)) ret = cur.fetchone() print(ret) cur.close() conn.close()
# 光标会记录位置 取得那就会记录此时的位置 import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='', database='daycs', charset='utf8', ) cursor = conn.cursor(pymysql.cursors.DictCursor) sql = "select * from book;" ret = cursor.execute(sql)#ret 受影响的行数 print(ret)#ret 受影响的行数 cursor.scroll(1,'absolute') #绝对移动,按照数据最开始位置往下移动1条 print(cursor.fetchmany()) #取出多条 默认取出1条 # print(cursor.fetchone()) #取出单条 # print(cursor.fetchall()) #取出所有的 cursor.scroll(3,'absolute') #绝对移动,按照数据最开始位置往下移动3条 cursor.scroll(3,'relative') #相对移动,按照当前光标位置往下移动3条 cursor.close()#关闭游标 conn.close()#关闭连接
# 光标会记录位置 取得那就会记录此时的位置 import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='', database='daycs', charset='utf8', ) cursor = conn.cursor(pymysql.cursors.DictCursor) sql = "insert into book values(%s,'eg','知识没有用出版社',200,'2047-7-5');" ret = cursor.execute(sql,('独孤九')) print(ret)#ret 受影响的行数 conn.commit()# 增删改都必须进行提交操作(commit)
文件内容
倚天屠龙记 egon 北京工业地雷出版社 70 20190701 九阳神功 alex 人民音乐不好听出版社 5 20180704 九阴真经 yuan 北京工业地雷出版社 62 20170712 九阴白骨爪 jinxin 人民音乐不好听出版社 40 20190807 独孤九剑 alex 北京工业地雷出版社 12 20170901 降龙十巴掌 egon 知识产权没有用出版社 20 20190705 葵花宝典 yuan 知识产权没有用出版社 33 20190802 学python从开始到放弃|alex|人民大学出版社|50|20180701 学mysql从开始到放弃|egon|机械工业出版社|60|20180603 学html从开始到放弃|alex|机械工业出版社|20|20180401 学css从开始到放弃|wusir|机械工业出版社|120|20180502 学js从开始到放弃|wusir|机械工业出版社|100|20180730
利用python操作
#第一步手动创建表 # create table book( # id int primary key , # book_name char(20) not null, # author char(12) not null, # press char(20) not null, # price float(6,2), # pub_date date # ); # 写入数据 import pymysql conn = pymysql.Connection(host='127.0.0.1', user='root', password="", database='daycs') cur = conn.cursor()#右边 with open('book.txt',encoding='utf-8') as f: try: for line in f: line = line.strip() if '\t' in line: lst = line.split('\t') elif '|' in line:, lst = line.split('|') sql = 'insert into book(book_name,author,press,price,pub_date) values (%s,%s,%s,%s,%s);' cur.execute(sql,lst) except Exception: conn.rollback()#操作不成功 回滚 conn.commit() cur.close() conn.close() # select book_name,price from book where author = 'egon' # 2.找出最贵的图书的价格 # select max(price) from book; # select price,book_name from book order by price desc limit 1; # 3.求所有图书的均价 # select avg(price) from book; # 4.将所有图书按照出版日期排序 # select * from book order by pub_date; # 5.查询alex写的所有书的平均价格 # select avg(price) from book where author = 'alex' # 扩展: 求所有人自己出版的图书的平均价格 # select author,avg(price) from book group by author # 扩展: 求所有人自己出版的图书的平均价格>30的所有人 # select author from book group by author having avg(price)>30 # 6.查询人民音乐不好听出版社出版的所有图书 # select * from book where press = '人民音乐不好听出版社'; # 7.查询人民音乐出版社出版的alex写的所有图书和价格 # select * from book where press = '人民音乐不好听出版社' and author = 'alex'; # 8.找出出版图书均价最高的作者 # select author,avg(price) as avg_p from book group by author order by avg_p desc limit 1; # 9.找出最新出版的图书的作者和出版社 # select author,press from book order by pub_date desc limit 1; # 10.显示各出版社出版的所有图书 # select press,group_concat(book_name) from book group by press; # 11.查找价格最高的图书,并将它的价格修改为50元 # select max(price) from book; # update book set price = 50 where price = 70 # 12.删除价格最低的那本书对应的数据 # select min(price) from book; # delete from book where price = 5; # 13.将所有alex写的书作者修改成alexsb # update book set author = 'alexsb' where author = 'alex'; # 14.select year(publish_date) from book # 自己研究上面sql语句中的year函数的功能,完成需求: # 将所有2017年出版的图书从数据库中删除 # delete from book where year(publish_date) = 2017;