我正在使用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()
我遇到了一个问题,就是只有一名员工会出现,直到您看到,我注释掉了selected_date输入变量并移到了for循环之外。绝对最好将其置于循环之外,我现在看到了。尽管如此,在循环内部输入日期的行为仍不是我所期望的。
我的问题是,为什么该程序只返回一名员工,而不是每次迭代都要求输入“ Date:”?
编辑: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