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 ...
 
随机推荐
- 倒排索引构建算法BSBI和SPIMI
			
参考:https://blog.csdn.net/androidlushangderen/article/details/44889677 倒排索引 : 一般的索引检索信息的方式.比如原始的数据源假设 ...
 - 【原创】《从0开始学RocketMQ》—单机搭建
			
内容目录 1. RocketMQ是什么? 2. 下载并解压 3. 启动NameServer 4. 启动 Broker 5. 关闭消息队列 1. RocketMQ是什么? RocketMQ是一种消息队列 ...
 - python之计数统计
			
前言: 计数统计,简单的说就是统计某一项出现的次数.实际应用中很多需求都需要用到这个模型,如检测样本中某一值出现的次数.日志分析某一消息出现的频率.分析文件中相同字符串出现的概率等等.以下是实现的不同 ...
 - LightOj  1220   Mysterious Bacteria
			
题目大意: 给出一个x,求满足x = b^p,p最大是多少? 解题思路: x可以表示为:x = p1^e1 * p2^e2 * p3^e3 ....... * pn^en. p = gcd (e1,e ...
 - _bzoj1014 [JSOI2008]火星人prefix【Splay】
			
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1014 天,写kth()时,把判断条件k <= siz[ch[x][0]]错写成了k & ...
 - Cunning Gena CodeForces - 417D
			
Cunning Gena CodeForces - 417D 题意 先将小伙伴按需要的监视器数量排序.然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱.那么按顺序枚举小伙伴,用ans[ ...
 - android 系统的时间间隔和睡眠用哪个?
			
原文 : https://developer.android.com/reference/android/os/SystemClock.html SystemClock.elapsedRealtime ...
 - 多个文本框点击复制  zClip (ZeroClipboard)有关问题
			
<script type="text/javascript" src="js/jquery.min.js"$amp;>amp;$lt;/script ...
 - 完美单例宏定义(兼容ARC和MRC),项目中可以直接使用
			
单例模式: 1.永远只分配一块内存来创建对象 2.提供一个类方法, 返回内部唯一的一个对象(一个实例) 3.最好保证init方法也只初始化一次 写一个宏定义文件,传入宏定义函数名,自动生成符合类名的 ...
 - Scala基础篇-04 try表达式
			
1.try表达式 定义 try{} catch{} finally{} //例子 try{ Integer.parseInt("dog") }catch { }finally { ...