Python strptime值错误,但看起来一切正确

问题描述 投票:0回答:2

我有一个应该获取在输入框中输入日期的日期的函数,并使用它来计算该范围内的某些数据,例如:从yyyy-mm-dd到yyyy-mm-dd

我输入的数据我对所有YYYY-MM-DD都使用了这种格式(在SQLITE数据库中,我具有的功能以及输入框)在这里,我将分享导致错误的功能。

但是我仍然得到这个错误:

    raise ValueError("time data %r does not match format %r" %
ValueError: time data '' does not match format '%Y-%m-%d'
  def weekly_cal():
        connect = sqlite3.connect('working_time_app.db')
        cursor = connect.cursor()
        #fnishing i know it have a misspelling but this way should have no problem because i made that mistake
        #when i created the database and then was lazy to fix it so now this will be correct one
        cursor.execute("SELECT sum(fnishing - starting) FROM working_time_app_table WHERE date BETWEEN ? and ?",
                       ((datetime.strptime(d_u_entry, "%Y-%m-%d")), (datetime.strptime(d_f_entry, "%Y-%m-%d"))))
        #d_u_entry and d_f_entry are the variables which hold the value of my entrybox

        sum_result = cursor.fetchall()


        show_weekly_cal_label = Label(cal_win, text=sum_result, font=("mv boli", 12), fg="white", bg="black")
        show_weekly_cal_label.grid(column=3, row=15, columnspan=5)



        connect.commit()
        connect.close()
python python-3.x sqlite tkinter
2个回答
1
投票

如果working_time_app_table.date以格式为“ YYYY-MM-DD”的字符串存储在数据库中,则您希望将发送到WHERE date between ? and ?的参数设置为字符串。

strptime正在将条目转换为python日期/时间对象。由于输入的数据已经是格式为“ YYYY-MM-DD”的字符串,因此无需转换为任何内容。


0
投票

特别感谢所有建议调试的人。我调试确实有点压力,但最后我还是发现了自己的错误!

该错误与任何拼写错误或数据类型无关。问题是错误地找到了d_u_entry变量和d_f_entry变量。在这里,我将再次发布正确的代码形式。再次感谢大家以及您为帮助我投入的时间。也很抱歉打扰大家!!!

enter code here

  cursor.execute("SELECT total(finishing - starting) FROM working_time_app_table WHERE date BETWEEN ? and ?",
                   (d_f_entry, d_u_entry))

问题是我在开始日期之前输入了结束日期!!!

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.