博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python delete数据
阅读量:6005 次
发布时间:2019-06-20

本文共 1964 字,大约阅读时间需要 6 分钟。

#!/usr/bin/env python# -*- coding:utf-8 -*-# @Time   : 2017/11/24 0:27# @Author : lijunjiang# @File   : delete.pyimport MySQLdbselect_sql = '''    SELECT TID FROM (        (            SELECT Score.CouID, Course.TID, Teacher.TName, COUNT(Course.TID) as teacher_count FROM Score            JOIN Course ON Score.CouID = Course.CouID and Score.Grade < 60            JOIN Teacher ON Course.TID = Teacher.TID            GROUP BY Course.TID            ORDER by teacher_count DESC            LIMIT 5        )         as teacher_tid    )'''delete_sql = '''    delete from Teacher where TID in (        SELECT TID FROM (            (                SELECT Score.CouID, Course.TID, Teacher.TName, COUNT(Course.TID) as teacher_count FROM Score                JOIN Course ON Score.CouID = Course.CouID and Score.Grade < 60                JOIN Teacher ON Course.TID = Teacher.TID                GROUP BY Course.TID                ORDER by teacher_count DESC                LIMIT 5            )             as teacher_tid            )    )'''def connect_mysql():    info_mysql = {        'host': '11.11.11.11',        'port': 3306,        'db': 'python',        # 'charset':'utf8',        'user': 'python',        'passwd': 'python'    }    try:        cnx = MySQLdb.connect(**info_mysql)    except Exception as err:        raise err    return cnxif __name__ == '__main__':    cnx = connect_mysql()    try:        cus = cnx.cursor()        cus.execute(select_sql)        result = cus.fetchall()        print(result)        cus.execute(delete_sql)        cus.execute(select_sql)        result1 = cus.fetchall()        print(result1)        cus.close()        cnx.commit()    except Exception as err:        cnx.rollback()        raise err    finally:        cnx.close()

运行结果:

C:\Python27\python.exe D:/Python/Mysql/delete.py((10L,), (4L,), (3L,), (1L,), (9L,))((8L,), (7L,), (5L,), (6L,))Process finished with exit code 0

转载于:https://www.cnblogs.com/lijunjiang2015/p/7887858.html

你可能感兴趣的文章
压缩介绍、bz2、gz、xz压缩工具
查看>>
StretchRect...果然和文档上说的一样
查看>>
Python成生随机KEY工具
查看>>
将一个数组拆分为几个至少三个元素的递增子序列
查看>>
备忘,解决WIN10下COM注册问题
查看>>
SAP移动解决方案在零售行业的应用方案及案例分享
查看>>
cx_Oracle install
查看>>
jquery ajax从后台获取数据
查看>>
基于Windows平台TSM 6.x版本下,如何删除初始化失败的实例。
查看>>
Start Code School Today!
查看>>
Nginx下载服务生产服务器调优
查看>>
RHEL6.5_KVM_VLAN_SET
查看>>
Windows下GCC编译环境中文乱码解决方案
查看>>
linux cut
查看>>
Linux 下JDK 安装
查看>>
DNS详解之重新认识DNS一
查看>>
VC中绘图练习(第三方库)
查看>>
MySql增加用户、授权、修改密码等语句
查看>>
Esc键无法关闭modal页面
查看>>
Eclipse下JRebel插件配置
查看>>