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 ...
随机推荐
- NOIp 2017 奶酪 【并查集】 By cellur925
题目传送门 Orz去年考场上做这道题的我应该还在抱怨没学过空间几何,不一会太困了就开始打瞌睡,然后为了防止睡觉开始在devc++上写默写离骚(逃 思路:如果两个空洞相交,那么把他们并在一个集合里.最后 ...
- Flutter开发移动端APP的入门教程及简单介绍
Dart&Flutter环境搭建 安装 dart SDK 如果只开发移动应用,那么您不需要Dart SDK; 只需安装Flutter. 这里就直接安装 Flutter (dart SDK已经集 ...
- Centos 下php安装配置xdebug扩展
2018年05月02日 19:54:42 杨汉松 阅读数:44 1.下载安装xdebug 获取xdebug wget http://www.xdebug.org/files/xdebug-2.3. ...
- c语言程序设计案例教程(第2版)笔记(三)—变量、结构体
零散知识点: 变量 :C语言中,每个变量必须先定义后引用.所谓变量存在是指系统为这个变量分配一块存储空间,此时对变量的操作,就是对变量所对应的存储空间中存放的数据进行操作.人们将变量占据 ...
- mysql主从同步异常原因及恢复
mysql主从同步异常原因及恢复 前言 mysql数据库做主从复制,不仅可以为数据库的数据做实时备份,保证数据的完整性,还能做为读写分离,提升数据库的整体性能.但是,mysql主从复制经常会因为某些原 ...
- 洛谷 P3808 【模板】AC自动机(简单版)洛谷 P3796 【模板】AC自动机(加强版)
https://www.cnblogs.com/gtarcoder/p/4820560.html 每个节点的后缀指针fail指针指向: 例如he,she,his,hers的例子(见蓝书P214): 7 ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- Person p = new Person("zhangsan",20);该句话都做了什么事情?
1,因为new用到了Person.class.所以会先找到Person.class文件并加载到内存中.2,执行该类中的static代码块,如果有的话,给Person.class类进行初始化.3,在堆内 ...
- Java Annontation(注解)详解
java中经常用到注解(Annontation),索性整理了下关于注解的相关知识点: 一.概念 Annontation是Java5开始引入的新特征,类似与.NET 中的attribute.中文名称一般 ...
- 222 Count Complete Tree Nodes 完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...