python调用mysql存储过程

 
 

 数据库存储过程:

#统计出勤
drop procedure if exists jyf_test_py;
DELIMITER ;;
CREATE PROCEDURE jyf_test_py(IN date varchar(10),IN checkIn varchar(10),IN checkOut varchar(10))
BEGIN
        DECLARE time decimal(4,2);    
      if TIME_TO_SEC(checkIn)<=TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut)<=TIME_TO_SEC("18:00") THEN
                set time = FORMAT((TIME_TO_SEC("09:00") - TIME_TO_SEC(checkIn))/(60*60) + 7.5 - (TIME_TO_SEC("18:00") - TIME_TO_SEC(checkOut))/(60*60),2);
        ELSEIF TIME_TO_SEC(checkIn)<=TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut)>=TIME_TO_SEC("18:00") AND TIME_TO_SEC(checkOut)<=TIME_TO_SEC("18:40") THEN
                set time = FORMAT((TIME_TO_SEC("09:00") - TIME_TO_SEC(checkIn))/(60*60) + 7.5 ,2);
    
        ELSEIF TIME_TO_SEC(checkIn)<=TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut)>TIME_TO_SEC("18:40") AND TIME_TO_SEC(checkOut)>TIME_TO_SEC("06:00") THEN
                set time = FORMAT((TIME_TO_SEC("09:00") - TIME_TO_SEC(checkIn))/(60*60) + 7.5 + (TIME_TO_SEC(checkOut) - TIME_TO_SEC("18:40"))/(60*60),2);
        ELSEIF TIME_TO_SEC(checkIn)<=TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut)>TIME_TO_SEC("18:40") AND TIME_TO_SEC(checkOut)<TIME_TO_SEC("06:00") THEN
                set time = FORMAT((TIME_TO_SEC("09:00") - TIME_TO_SEC(checkIn))/(60*60) + 7.5 + 5.33 + TIME_TO_SEC(checkOut)/(60*60),2);
    
        ELSEIF TIME_TO_SEC(checkIn)>TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut) <= TIME_TO_SEC("10:00") AND TIME_TO_SEC(checkOut)<=TIME_TO_SEC("18:40") AND TIME_TO_SEC(checkOut)>TIME_TO_SEC("06:00") THEN
                set time = FORMAT((TIME_TO_SEC("12:00") - TIME_TO_SEC(checkTime))/(60*60) + 4.5  + (TIME_TO_SEC(checkOut) - TIME_TO_SEC("18:40"))/(60*60),2);
        ELSEIF TIME_TO_SEC(checkIn)>TIME_TO_SEC("09:00") AND TIME_TO_SEC(checkOut) <= TIME_TO_SEC("10:00") AND TIME_TO_SEC(checkOut)<=TIME_TO_SEC("18:40") AND TIME_TO_SEC(checkOut)<TIME_TO_SEC("06:00") THEN
                set time = FORMAT((TIME_TO_SEC("12:00") - TIME_TO_SEC(checkTime))/(60*60) +4.5 + 5.33 + (TIME_TO_SEC(checkOut))/(60*60),2);
      end if;
      INSERT INTO `jyf_test`.`attendance`(`date`, `checkIn`, `checkOut`, `time`) VALUES (date, checkIn, checkOut, time);
        INSERT INTO `jyf_test`.`attendance_test`(`date`, `checkIn`, `checkOut`, `time`) VALUES (date, checkIn, checkOut, time);
    COMMIT;
END ;;
DELIMITER ;
CALL jyf_test_py("2021-07-09","08:02","20:41");

 python调用存储过程

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#pip install requests
#pip install jsonpath
#pip install mysql
import requests
from jsonpath import jsonpath
import json
import time
import MySQLdb


def getAttend(url,header):
    
    #data = {"account": "companyG", "adminUserCode": "000009", "companyCode": "53c3aeab8c1e11e9898400163e138319", "password": "c4ca4238a0b923820dcc509a6f75849b", "timestamp": "20191009112205", "sign": "55efce47806e2056adfa0cb769c274c8"} 
    

    #res = requests.post(url,json=data,headers=header)
    res = requests.get(url,headers=header).json()


    #print(res.headers)  # 响应头
    #print(data)  # 输出响应的文本
    signinTime = jsonpath(res, '$.data.signinTime')[0][0:-3]
    signoutTime = jsonpath(res, '$.data.signoutTime')[0][0:-3]
    date = time.strftime("%Y-%m-%d", time.localtime())
    return date,signinTime,signoutTime

def CallProc(date,signinTime,signoutTime):
    conn = MySQLdb.connect(host='127.0.0.1',user='name',passwd='password',db='jyf_test')
    cur =conn.cursor()
    cur.callproc('jyf_test_py',(date,signinTime,signoutTime))
    conn.commit()
    cur.close()
    conn.close();

url = 'https://127.0.0.1/attend/current-sign-status'
header = {"authorization": "Basic MTUxMTIzMTk6MzQ2NTE1YmU4MTkwZDdiNDA2NWJlODMwMzRiY2Q5NDQ="}

#获取日期、签到时间、签退时间
date,signinTime,signoutTime = getAttend(url,header)
print(date,signinTime,signoutTime)
#print(type(date),type(signinTime),type(signoutTime))
#调用存储过程
if not (signinTime is None) and not (signoutTime is None):
    CallProc(date,signinTime,signoutTime)
原文地址:https://www.cnblogs.com/ai594ai/p/14991353.html