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()
python 链接mysql 修改查询删除语句的更多相关文章
- python链接mysql
1.安装MySQLdb MySQLdb 是用于Python链接Mysql数据库的接口,它实现了 Python 数据库 API 规范 V2.0,基于 MySQL C API 上建立的. 下载地址: ht ...
- python链接mysql pymysql
python链接mysql import pymysql conn = pymysql.connect(user=', database='gbt2019', charset='utf8') curs ...
- python 链接mysql 连接池
# python 链接mysqlimport mysql.connector.poolingconfig = { "host":"localhost", &qu ...
- python 链接mysql
下载对应版本 安装 https://dev.mysql.com/downloads/connector/python/ 创建链接 # python 链接mysqlimport mysql.conn ...
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- Python --链接MYSQL数据库与简单操作 含SSH链接
项目是软硬件结合,在缺少设备的情况,需要通过接口来模拟实现与设备的交互,其中就需要通过从数据库读取商品的ID信息 出于安全考虑 现在很多数据库都不允许通过直接访问,大多数是通过SSH SSH : 数 ...
- 查找Mysql慢查询Sql语句
一.MySQL数据库有几个配置选项可以帮助我们及时捕获低效SQL语句 1,slow_query_log 这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句. 2,long_query_tim ...
- Python操作Mysql数据库时SQL语句的格式问题
一.概述 近日使用Python对Mysql数据库进行操作,遇到SQL语句死活出问题的情况.由于最初没有将异常打印出来,一直不知道原因.随后,将异常打印出来之后,通过异常信息,对代码进行修改.最终,成功 ...
- 工作随笔——mysql子查询删除原表数据
最近在开发的时候遇到一个mysql的子查询删除原表数据的问题.在网上也看了很多方法,基本也是然并卵(不是写的太乱就是效率太慢). 公司DBA给了一个很好的解决方案,让人耳目一新. DELETE fb. ...
随机推荐
- 51nod 1402 最大值(贪心)
原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 思路:借鉴了这篇博文http://blog.csdn.n ...
- [清华集训2016]温暖会指引我们前行——LCT+最大生成树
题目链接: [清华集训2016]温暖会指引我们前行 题目大意:有$n$个点$m$次操作,每次操作分为三种:1.在$u,v$两点之间连接一条编号为$id$,长度为$l$,温度为$t$的边.2.查询从$u ...
- java通讯录获取汉字首字母
1.本文只是使用了pinyin4J的主要功能,还有更多更好耍的功能,大家可以去研究官网文档.哈哈 2.pinyin4j的官方下载地址:https://sourceforge.net/projects/ ...
- 揭开HTTPS的神秘面纱
摘自:https://www.cnblogs.com/hujingnb/p/11789728.html 揭开HTTPS的神秘面纱 在说HTTP前,一定要先介绍一下HTTP,这家伙应该不用过多说明了 ...
- 网页中JS函数自动执行常用三种方法
(1)最简单的调用方式,直接写到html的body标签里面: <body onload="myFunction()"></body> ...
- Mysql 中需不需要commit
摘自:https://blog.csdn.net/zzyly1/article/details/81003122 mysql在进行增删改操作的时候需不需要commit,这得看你的存储引擎, 如果是不支 ...
- centos6 yum安装mysql 5.6 (完整版)
使用源代码编译安装mysql还是比较麻烦,一般来说设备安装时请网络同事临时开通linux上网,通过yum网络实现快速安装,或配置yum仓库进行内网统一安装. 通过网络快速安装过程如下 一.检查系统是否 ...
- Uep的保存操作
wzStoreInfoDefineService.update(neWzStoreInfo,updateList,insertList,deleteListpublic void update(WzS ...
- Boston House Price with Scikit-Learn
Boston House Price with Scikit-Learn Data Description >>> from sklearn.datasets import load ...
- 【转】How-to: Enable User Authentication and Authorization in Apache HBase
With the default Apache HBase configuration, everyone is allowed to read from and write to all table ...