zoukankan      html  css  js  c++  java
  • 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()
           
     
  • 相关阅读:
    sql函数 StringSplit(SELECT * from Split('黄色,蓝色,黑色',','))
    跨表循环写插入sql语句
    将ExCel导入数据库
    行转列
    js正则匹配
    ASP.NET下载远程图片保存到本地的方法、保存抓取远程图片
    C# 解析json类型字符串
    上传图片
    存储过程学习
    //js验证数字输入,以及保留俩位
  • 原文地址:https://www.cnblogs.com/lzcys8868/p/7888761.html
Copyright © 2011-2022 走看看