python操作MySQL之pymysql模块

import pymysql#pip install pymysql
db=pymysql.connect('localhost','root','123456','day040')
cursor=db.cursor()  #创建游标

book_list=["学python从开始到放弃|alex|人民大学出版社|50|2018-7-1",
           "学mysql从开始到放弃|egon|机械工业出版社|60|2018-6-3",
           "学html从开始到放弃|alex|机械工业出版社|20|2018-4-1",
           "学css从开始到放弃|wusir|机械工业出版社|120|2018-5-2",
           "学js从开始到放弃|wusir|机械工业出版社|100|2018-7-30"]

# for row in book_list:
#     book=row.split("|")
#     book[-2]=float(book[-2])
#     book=tuple(book)
#     sql=f" insert into book2(b_name,b_author,b_press,b_price,publish_date) values {book};"
#     cursor.execute(sql)     #执行语句,只在内存执行
#     db.commit()           #提交给数据库修改(增删改需要提交才能完成)



sql='select * from  book2'
cursor.execute(sql)


# 获取一条:fetchone()
# re=cursor.fetchone()
# print(re)


# 获取部分fetchmany(n)
# results=cursor.fetchmany(3)
# print(results)


# 获取所有fetchall()
results=cursor.fetchall()
for row in results:
    for i in row:
        print(i,end='')
    print()


db.close()

 

posted @ 2019-08-27 11:57  笑得好美  阅读(273)  评论(0编辑  收藏  举报