加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:59:47 所属栏目:Python 来源:网络整理
导读:import mysql.connector.poolingconfig = { "host": "localhost","port": 3306,"user": "root","password": "","database": "demo"}try: pool = mysql.connector.pooling.MySQLConnectionPool( **config,pool_size=10 ) con = pool.get_connection() con.sta
import mysql.connector.poolingconfig = {    "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()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读