本文参见这里

使用缓冲的 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 接口 (三)的更多相关文章

  1. MySQL Connector/Python 接口 (一)

    这里仅介绍 MySQL 官方开发的 Python 接口,参见这里: https://dev.mysql.com/doc/connector-python/en/ Chapter 1 Introduct ...

  2. MySQL Connector/Python 接口 (二)

    连接数据库 本文参见这里,示例如何连接MySQL 数据库. import mysql.connector from mysql.connector import errorcode # 连接数据库需要 ...

  3. 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 ...

  4. 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 ...

  5. MySQL Connector/Python 安装、测试

         安装Connector/Python: # wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-pyth ...

  6. python使用mysql的三个模块:mysql.connector、sqlalchemy、MySQLdb

    在python中使用mysql其实很简单,只要先安装对应的模块即可,那么对应的模块都有什么?官方也没指定也没提供,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy ...

  7. MySQL:Download Connector/Python

    MySQL Connector / Python是用于Python平台和开发的标准化数据库驱动程序. 此外,MySQL Connector / Python 8.0支持使用MySQL Server 8 ...

  8. Python:安装MYSQL Connector

    在Python中安装MySQL Connector有如下三种方法: 1.直接安装客户端[建议使用] pip install mysqlclient 2.安装mysql连接器 pip install - ...

  9. mysql.connector操作mysql的blob值

    This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and re ...

随机推荐

  1. java 继承还是组合

    1.我真的需要上溯转型(upcasting)吗? 如果是,才会用到继承. 2.

  2. SSM-避坑记(一)

    在运行测试类的时候的报错,内容如下 报错: class path resource [spring/spring-search.xml] cannot be opened because it doe ...

  3. Python实现判断回文串

    回文数的概念:即是给定一个数,这个数顺读和逆读都是一样的.例如:121,1221,a,aa是回文数,123,1231不是回文数.  while 1: String = input('请先输入一个字符串 ...

  4. [POI2007]旅游景点atr

    Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之 ...

  5. _bzoj1010 [HNOI2008]玩具装箱toy【斜率优化dp】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1010 裸的斜率优化,第一次写队首维护的时候犯了个智障错误,队首维护就是维护队首,我怎么会那队 ...

  6. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  7. 题解报告:hdu 1754 I Hate It(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某 ...

  8. Mac OS X:在标题栏上显示目录完整路径

    众所周知mac的finder是不带路径显示的,你进入某个文件夹只会显示当前文件夹的名字而已.虽然你可以在finder的菜单栏中点“显示”-“显示路径栏”把路径栏调出来,但是这样只会不必要的增加find ...

  9. python的des和3des加解密

    1.加密: pyDes.des(key, [mode], [IV], [pad], [padmode]) pyDes.triple_des(key, [mode], [IV], [pad], [pad ...

  10. EL表达式、JSTL

    EL表达式 一.简介 > JSP表达式 <%= %> 用于向页面中输出一个对象.        > 到JSP2.0时,在我们的页面中不允许出现 JSP表达式和 脚本片段.   ...