• python 链接mysql 修改查询删除语句


    import mysql.connector.pooling

    config = {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "",
    "database": "demo"
    }

    try:
    pool = mysql.connector.pooling.MySQLConnectionPool(
    **config,
    pool_size=10
    )

    con = pool.get_connection()
    con.start_transaction()
    cursor = con.cursor()
    # 复制表结构
    # sql= "create table t_emp_new like t_emp "
    # cursor.execute(sql)


    sql = "select avg(sal) as avg from t_emp"
    cursor.execute(sql)
    # 取一条记录
    temp = cursor.fetchone()
    # 平均底薪
    avg = temp[0]

    sql = " select deptno from t_emp group by deptno having avg(sal)> %s"
    cursor.execute(sql,[avg])
    # 取出所有记录
    temp = cursor.fetchall()
    # print(temp)

    # sql = "insert into t_emp_new select * from t_emp where deptno in ( "
    # for index in range(len(temp)):
    # one = temp[index][0]
    # if index < len(temp) -1 :
    # sql+= str(one)+ ","
    # else:
    # sql += str(one)
    # sql += ")"
    # # print(sql) insert into t_emp_new select * from t_emp where deptno in ( 10,20)
    # cursor.execute(sql)

    # sql = "delete from t_emp where deptno in ("
    # for index in range(len(temp)):
    # one = temp[index][0]
    # if index < len(temp)-1:
    # sql += str(one)+ ","
    # else:
    # sql += str(one)
    # sql += " )"
    # # delete from t_emp where deptno in (10, 20)
    # # print(sql)
    # cursor.execute(sql)

    # 查询部门 编号
    sql = " select deptno from t_dept where dname = %s"
    cursor.execute(sql,['SALES'])
    deptno = cursor.fetchone()
    # print(deptno[0]) 30

    sql = "update t_emp_new set deptno = %s"
    cursor.execute(sql,[deptno[0]])

    con.commit()

    except Exception as e:
    print(e)
    if 'con' in dir():
    con.close()
  • 相关阅读:
    Cocos2d-x教程(34)-三维物体OBB碰撞检測算法
    POJ 2485 Highways 最小生成树 (Kruskal)
    LintCode-分糖果
    云存储市场布局已定,怎样助力企业互联网转型
    HDU 1853 Cyclic Tour(最小费用最大流)
    windows下基于bat的每1分钟执行一次一个程序
    python中匹配中文,解决不匹配,乱码等问题
    bytes,packet区别 字节数据包
    wmic
    paramiko 模块封装
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11356170.html
Copyright © 2020-2023  润新知