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()

python 链接mysql 修改查询删除语句的更多相关文章

  1. python链接mysql

    1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: ht ...

  2. python链接mysql pymysql

    python链接mysql import pymysql conn = pymysql.connect(user=', database='gbt2019', charset='utf8') curs ...

  3. python 链接mysql 连接池

    # python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...

  4. python 链接mysql

    下载对应版本   安装 https://dev.mysql.com/downloads/connector/python/ 创建链接 # python 链接mysqlimport mysql.conn ...

  5. python学习道路(day12note)(mysql操作,python链接mysql,redis)

    1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...

  6. Python --链接MYSQL数据库与简单操作 含SSH链接

    项目是软硬件结合,在缺少设备的情况,需要通过接口来模拟实现与设备的交互,其中就需要通过从数据库读取商品的ID信息 出于安全考虑  现在很多数据库都不允许通过直接访问,大多数是通过SSH SSH : 数 ...

  7. 查找Mysql慢查询Sql语句

    一.MySQL数据库有几个配置选项可以帮助我们及时捕获低效SQL语句 1,slow_query_log 这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句. 2,long_query_tim ...

  8. Python操作Mysql数据库时SQL语句的格式问题

    一.概述 近日使用Python对Mysql数据库进行操作,遇到SQL语句死活出问题的情况.由于最初没有将异常打印出来,一直不知道原因.随后,将异常打印出来之后,通过异常信息,对代码进行修改.最终,成功 ...

  9. 工作随笔——mysql子查询删除原表数据

    最近在开发的时候遇到一个mysql的子查询删除原表数据的问题.在网上也看了很多方法,基本也是然并卵(不是写的太乱就是效率太慢). 公司DBA给了一个很好的解决方案,让人耳目一新. DELETE fb. ...

随机推荐

  1. Python3学习笔记(十):赋值语句和布尔值

    一.赋值语句 1.序列解包 多个赋值同时进行: >>> x,y,z = 1, 2, 3 >>> print(x, y, z) 1 2 3 变量交换: >> ...

  2. Spring Boot教程(二十八)通过JdbcTemplate编写数据访问

    数据源配置 在我们访问数据库的时候,需要先配置一个数据源,下面分别介绍一下几种不同的数据库配置方式. 首先,为了连接数据库需要引入jdbc支持,在pom.xml中引入如下配置: <depende ...

  3. Jmeter连接Redis服务缓存

    1.添加线程组->Sampler->BeanShell Sampler,加入以下内容: import redis.clients.jedis.Jedis; import org.apach ...

  4. phpstorm 设置ftp自动保存服务器 (原)

    打开PHPstorm,依次  tools -  deployment  --  configuration 配置ftp或者sftp地址用户名密码等 端口号 要不就是 21 要不就是 22 , 22不行 ...

  5. C++入门经典-例6.14-通过指针连接两个字符数组

    1:字符数组是一个一维数组,引用字符数组的指针为字符指针,字符指针就是指向字符型内存空间的指针变量. char *p; char *string="www.mingri.book" ...

  6. C++入门经典-例6.2-将二维数组进行行列对换

    1:一维数组的初始化有两种,一种是单个逐一赋值,一种是使用聚合方式赋值.聚合方式的例子如下: int a[3]={1,2,3}; int a[]={1,2,3};//编译器能够获得数组元素的个数 in ...

  7. C++入门经典-例4.7-变量的作用域

    1:代码如下: // 4.7.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...

  8. 【Spark机器学习速成宝典】模型篇08支持向量机【SVM】(Python版)

    目录 什么是支持向量机(SVM) 线性可分数据集的分类 线性可分数据集的分类(对偶形式) 线性近似可分数据集的分类 线性近似可分数据集的分类(对偶形式) 非线性数据集的分类 SMO算法 合页损失函数 ...

  9. Python学习笔记:类

    类可以将数据与函数封装起来,用一个例子解释,先定义一个类: class athlete: def __init__(self,a_name,a_dob=None,a_times=[]): self.n ...

  10. 2.进行model和log的路径创建

    第一步:使用datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') 用于生成当前时间 第二步: 使用os.path.join() 将文件的路径与subdi ...