本文参见这里

使用缓冲的 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. Python中处理日期时间库的使用方法(转载)

    <本文来自公众号“大邓带你玩python”,转载> 用百分之20时间,学会解决百分之80的问题. 常用的库有time.datetime.其中datetime库是对time库的封装,所以使用 ...

  2. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  3. $.extend(x,y); 函数用法介绍。

    第一篇资料:  转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...

  4. AJPFX关于Java NIO的概述总结

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...

  5. css的过渡背景色

    css3新增的渐变背景色属性用法 原博客地址:http://caibaojian.com/css3-background-gradient.html

  6. struts2 <allowed-methods > 标签配置

    1.在struts2   2.5版本中添加了对方法访问的权限,如果没有被添加到<allow-method> 方法的标签,将会报一下错误 5:05:18.078 [http-apr-8020 ...

  7. JavaScript回文数

    基本解决方案 function palindrome(str) { return str.replace(/[\W_]/g, '').toLowerCase() === str.replace(/[\ ...

  8. bzip2 一种块排序文件压缩软件

    总览 bzip2 [ -cdfkqstvzVL123456789 ] [ filenames ... ] bunzip2 [ -fkvsVL ] [ filenames ... ] bzcat [ - ...

  9. 第一天 初识Python

    Python基础 一 编程语言     什么是编程语言?    上面提及的能够被计算机所识别的表达方式即编程语言,语言是沟通的介质,而编程语言是程序员与计算机沟通的介质.在编程的世界里,计算机更像是人 ...

  10. MFCEditBox如何自动换行

    设置该EditBox属性: 1.Auto HScroll             False 2.OEM Convert           False 3.Want Return           ...