数据比较工具按日期输入请求中断:

2024-06-10 23:27:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用sqlite3开发一个Python程序,该程序将不同员工的计费工时与现场工时进行比较,代码如下:

from dbConnect import db_connect


con = db_connect()
cur = con.cursor()


def hours_discrepancy():

    emp_ids_dict = [4759, 4477, 4475, 1230, 4479]
    continue_marker = "yes"
    chosen_date = input("Date: ")

    for i in emp_ids_dict:

        choose_emp = i
        # chosen_date = input("Date: ")

        emp_check = cur.execute(f"SELECT LastName, FirstName "
                                f"FROM BrandHours "
                                f"WHERE EmpID LIKE {choose_emp} AND Date LIKE {chosen_date}")
        emp_fetch = emp_check.fetchone()
        employee_name = str(emp_fetch[0] + ", " + emp_fetch[1])
        print(employee_name)

        reported = cur.execute(f"SELECT Reported, Punched "
                               f"FROM BrandHours "
                               f"WHERE EmpID LIKE {choose_emp} AND Date LIKE {chosen_date}")

        hours_fetch = reported.fetchone()

        reported_hours = hours_fetch[0]
        punched_hours = hours_fetch[1]

        print(reported_hours)
        print(punched_hours)

        discrepancy = reported_hours - punched_hours
        print(f"Discrepancy: {discrepancy}")
        print("=============================================================================")


hours_discrepancy()

我遇到了一个问题,在这个问题中,只有一个员工会出现,正如您所看到的,直到我注释掉所选的\u date输入变量,并移到for循环之外。我现在明白了,最好是把它放在圈外。不过,在循环中输入日期的行为并不是我所期望的

我的问题是,为什么程序只返回一个员工,而不是在每次迭代中要求输入“Date:

EDIT:dbConnect是我的另一个脚本,它调用:

import os
import sqlite3


default_path = os.path.join(os.path.dirname(__file__), 'db.sqlite3')


def db_connect(db_path=default_path):
    con = sqlite3.connect(db_path)
    return con

Tags: pathdbdateconnectfetchconsqlite3like