python 基础 9.11 更改数据

#/usr/bin/python
#-*- coding:utf-8 -*-
#@Time   :2017/11/24 4:45
#@Auther :liuzhenchuan
#@File   :更改数据.py
 
import MySQLdb
 
# 把分数小于5分的成绩都加6分
sql = '''select *, (grade+60) as new_grade from score where grade < 5;'''
update_sql = '''update score set grade = grade + 60 where grade <5;'''
 
def connect_mysql():
    db_config = {
        "host": "192.168.16.70",
        "port": 3306,
        "user": "root",
        "passwd": "123123",
        "db": "students",
        "charset": "utf8"
    }
    try:
        cnx = MySQLdb.connect(**db_config)
    except Exception as e:
        raise e
    return cnx
 
if __name__ == "__main__":
    cnx = connect_mysql()
    cus = cnx.cursor()
    try:
        cus.execute(sql)
        result1 = cus.fetchall()
        print(result1)
        cus.execute(update_sql)
        cus.execute(sql)
        result2 = cus.fetchall()
        print(result2)
        cus.close()
        cnx.commit()
    except Exception as e:
        cnx.rollback()
        raise e
    finally:
        cnx.close()
       
 
posted @ 2017-11-24 04:52  刘振川的博客  阅读(239)  评论(0编辑  收藏  举报