zoukankan      html  css  js  c++  java
  • 10pymysql使用方法

    1使用流程

    1建立数据库连接(db=pymysql.connect(...))

    2创建游标对象(cur=db.cursor())

    3游标方法:cur.execute("insert...")

    4提交到数据库或者获取数据:db.commit()/db.fetchall()

    5关闭游标对象:cur.close()

    6关闭数据库连接:db.close()

    2cur方法

    name = input(">>")
    sql = "select name,score from cls where name=%s or score > %s;"
    cur.execute(sql,[name,85]) #第二个位置只能放参量,参量指我们查询时的条件

    cur.fetchone()
    cur.fetchmany(10)
    cur.fetchall
     

    写操作

    import pymysql
    
    db = pymysql.connect(host='localhost',port=3306,user='root',password='159357',database='books',charset='utf8')
    cur = db.cursor()
    try:
        sql = "update book set price=33 where title=%s"
        cur.execute(sql,["边城"])
        db.commit()
    except Exception as e:
        print(e)
        db.rollback()
    cur.close()
    db.close()
  • 相关阅读:
    Visual Studio2019安装步骤
    写在第一页的话
    数状数组
    hdu 3501 数学题
    静态邻接表
    最长子序列
    hdu 1094 所想到的
    bellman_ford
    郁闷的一晚
    SPFA + 静态邻接表 模板
  • 原文地址:https://www.cnblogs.com/gao-chao/p/13256366.html
Copyright © 2011-2022 走看看