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()
原文地址:https://www.cnblogs.com/gao-chao/p/13256366.html