python 操作数据库

痴心易碎 提交于 2019-11-26 06:37:22
import pymysql

db=pymysql.connect(host,user,passwd,database)
cursor=db.cursor(pymysql.cursors.DictCursor) #结果以字典返回
sql="select avg(salary) from department group by depart order by avg(salary) desc limit(5,6)"
try:    
    cursor.execute(sql)    
    cursor.commit()  # 增删改操作需要提交    
    result=cursor.fetchall()    
    print(result)
except:    
    cursor.rollback() # 增删改操作失败需要回退    
    print("查找失败")
cursor.close()
db.close()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!