zoukankan      html  css  js  c++  java
  • 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()
    
    
  • 相关阅读:
    csu1217: 奇数个的那个数
    小试SAS 判别分析
    bfs poj2965
    STL set常用操作
    csu1002 A+B(III)
    HDOJ 1002 的几种方法
    SQL知识积累
    CSV文件格式介绍
    ASP.net Web Form 知识积累
    C# 位域[flags] 枚举
  • 原文地址:https://www.cnblogs.com/LinTeX9527/p/7751871.html
Copyright © 2011-2022 走看看