循环用数据做奇怪的事情

时间:2018-05-31 10:00:10

标签: python sql loops tkinter

好的,所以我现在已经解决了这个问题大约8个小时了,我无法弄清楚发生了什么。所以我正在制作的应用程序是一个使用Android应用程序的TimeSheet管理器;我在python中制作的桌面应用程序;和一个MySQL数据库。桌面应用程序几乎只使用数据库上的查询。因此,当前查询是供用户检查员工在两个日期之间完成的小时数:

query = "SELECT FirstName, LastName, StartTime, FinishTime FROM Sheets WHERE DateTesting >= '"+StartDate+"' AND DateTesting <= '"+EndDate+"';"

然后python应用程序通过使用日期时间来显示结果,以获取开始时间和结束时间,并计算出员工工作的小时数。这工作正常。

        tdelta = datetime.datetime.strptime(s2, FMT) - datetime.datetime.strptime(s1, FMT)
        tdelta=(str(tdelta))
        totalDelta.append(tdelta)

        self.labels.append(Label(myframe,text=f'Name: {a} {b} | Start Time: {c} | Finish Time: {d} | Hours Done: {tdelta}'))
        self.labels[q].grid(column=0, row=+r)

仅供参考,它使用循环来循环访问信息。下一部分(我遇到的问题)是计算每个员工完成的总时数,但是我的循环很糟糕,而且我正处于被大脑阻塞的状态。

    for u in secondNameList:
        totalHoursPerPerson = 0
        totalMinutesPerPerson = 0    
        for item in nameList:
            if item == u:
                timeParts = [int(s) for s in tdelta.split(':')]
                totalMinutesPerPerson += timeParts[1]
                totalHoursPerPerson += timeParts[0] 
                totalHoursPerPerson += totalMinutes // 60
                minutesToTake = totalMinutes // 60
                minutesToSub = minutesToTake * 60
                totalMinutesPerPerson = totalMinutes - minutesToSub
                print(totalHoursPerPerson)
                totalsPerPerson.append(f'{item} did a total of {totalHoursPerPerson} Hours')
                print(f'{item} did a total of {totalHoursPerPerson} Hours')

    totalsPerPerson = list(set(totalsPerPerson))
    print(totalsPerPerson)

输出如下所示:

4
['Chris', 'Chris', 'Rory', 'John']
['Chris', 'Chris', 'Rory', 'John']
(('Chris', 'Gracey', '07:00', '16:30'), ('Chris', 'Gracey', '07:00', '16:30'), ('Rory', 'Gracey', '07:00', '16:30'), ('John', 'Smith', '07:00', '16:30'))
9
Chris did a total of 9 Hours
18
Chris did a total of 18 Hours
9
Chris did a total of 9 Hours
18
Chris did a total of 18 Hours
9
Rory did a total of 9 Hours
9
John did a total of 9 Hours
['John did a total of 9 Hours', 'Chris did a total of 18 Hours', 'Rory did a total of 9 Hours', 'Chris did a total of 9 Hours']

我真的不确定为什么它会两次展示克里斯。我也尝试删除重复项,但它只是打破了它。

如果有更好的方法,我会全力以赴。

由于

1 个答案:

答案 0 :(得分:0)

好的,所以我更改了sql语句。

这是我用过的陈述:

func postToWatson () {
            print("post to watson called")
            let url: String =  "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19"
            let message = "All you need is love"
            var parameters = [
               "username":"my-username",
                "password":"my-password"]
            parameters["text"] = message
            Alamofire.request(url, parameters: parameters)
                .responseJSON { response in
                    print(response.request)
                    print(response.response)
                    print(response.result)
            }
        }

当然在python中设置变量。