MySQL Connector/Python 接口 (三)
本文参见这里。
使用缓冲的 cursor,下例给从2000年加入公司并且还在公司的员工薪水从明天起加15%
from __future__ import print_function
from decimal import Decimal
from datetime import datetime, date, timedelta
import mysql.connector
tomorrow = datetime.now().date() + timedelta(days=1)
print("明天日期是: {}".format(tomorrow))
# 连接到服务器
cnx = mysql.connector.connect(user='lintex9527', password='lintex9527',database='employees')
# 获得两个缓冲的cursor
curA = cnx.cursor(buffered=True)
curB = cnx.cursor(buffered=True)
# 查询在指定日期加入公司的员工
query = (
"SELECT s.emp_no, salary, from_date, to_date FROM employees AS e "
"LEFT JOIN salaries AS s USING (emp_no) "
"WHERE to_date=DATE('9999-01-01') "
"AND e.hire_date BETWEEN DATE(%s) AND DATE(%s)")
# UPDATE and INSERT statements for the old and new salary
update_old_salary = (
"UPDATE salaries SET to_date = %s "
"WHERE emp_no=%s AND from_date=%s")
insert_new_salary = (
"INSERT INTO salaries (emp_no, from_date, to_date, salary) "
"VALUES (%s, %s, %s, %s)")
# Select the employees getting a raise
curA.execute(query, (date(2000, 1, 1), date(2000, 12, 30)))
# Iterate through the result of curA
for (emp_no, salary, from_date, to_date) in curA:
# update the old and insert the new salary
new_salary = int(round(salary * Decimal('1.15')))
curB.execute(update_old_salary, (tomorrow, emp_no, from_date))
curB.execute(insert_new_salary, (emp_no, tomorrow, date(9999, 1,1,), new_salary))
# 提交操作
cnx.commit()
print("操作完成,关闭链接")
cnx.close()
MySQL Connector/Python 接口 (三)的更多相关文章
- MySQL Connector/Python 接口 (一)
这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...
- MySQL Connector/Python 接口 (二)
连接数据库 本文参见这里,示例如何连接MySQL 数据库. import mysql.connector from mysql.connector import errorcode # 连接数据库需要 ...
- Snippet: Fetching results after calling stored procedures using MySQL Connector/Python
https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Pytho ...
- Installing MySQL Connector/Python using pip v1.5
The latest pip versions will fail on you when the packages it needs to install are not hosted on PyP ...
- MySQL Connector/Python 安装、测试
安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-pyth ...
- python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb
在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...
- MySQL:Download Connector/Python
MySQL Connector / Python是用于Python平台和开发的标准化数据库驱动程序. 此外,MySQL Connector / Python 8.0支持使用MySQL Server 8 ...
- Python:安装MYSQL Connector
在Python中安装MySQL Connector有如下三种方法: 1.直接安装客户端[建议使用] pip install mysqlclient 2.安装mysql连接器 pip install - ...
- mysql.connector操作mysql的blob值
This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...
随机推荐
- Luogu P1144 最短路计数 【最短路】 By cellur925
题目传送门 常规的最短路计数问题:注意有重边(重边不用理,看样例),自环(读入时过滤). 另外这个无向图没有权,其实可以直接bfs做,但考虑到以后带权的情况,按spfa走了. 水题被卡了三次(嘤嘤嘤 ...
- ObjectARX2012错误1 fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory; fatal error C1083: 无法打开包括文件:“map”: No such file or directory
问题1:fatal error C1083: 无法打开包括文件:“arxHeaders.h”: No such file or directory: 解决办法:这个问题很明显,是因为没有在工程属性里包 ...
- CF916E
Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...
- js中toFixed重写
在测试原生的toFixed发现,它在个浏览器上表现不一致,并且有些值在保留小数时得到的结果并不是想要,如在chrome下测试: 所以针对toFixed方法不准的问题,我们进行方法改造: 主要思路是:对 ...
- Android 给按钮添加监听事件
在安卓开发中,如果要给一个按钮添加监听事件的话,有以下三种实现方式 1.方式一 public class MainActivity extends ActionBarActivity { @Overr ...
- spark 学习路线及参考课程
一.Scala编程详解: 第1讲-Spark的前世今生 第2讲-课程介绍.特色与价值 第3讲-Scala编程详解:基础语法 第4讲-Scala编程详解:条件控制与循环 第5讲-Scala编程详解:函数 ...
- R Programming week1-Data Type
Objects R has five basic or “atomic” classes of objects: character numeric (real numbers) integer co ...
- linux下redis安装访问
下载编译安装 wget http://download.redis.io/releases/redis-3.0.1.tar.gz tar xvf redis-3.0.1.tar.gz mv redis ...
- 架构(Architecture)随想
架构(Architecture)的意义: 先不要看什么是架构,先看下architect是什么,没有错,它是建筑师,在一块空地上build高楼大厦的人,它是一个designer,设计好整个大楼,也是一个 ...
- tomcat不打印日志
commons-logging.jar导入这个包到tomcat lib下 2.修改tomcat的bin目录下面的catalina.bat文件 只需修改:set CLASSPATH=%CLASSP ...