zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    Filter and Listener
    Cookie Session
    怎样快速将一台电脑虚拟机导入另外一台电脑
    网页下载文件服务端代码
    图片验证码生成代码
    Request实战
    Response
    设置页眉页脚的距离
    页眉页脚的设置
    节的设置
  • 原文地址:https://www.cnblogs.com/open-yang/p/11414907.html
Copyright © 2011-2022 走看看