Python strTime值错误,但一切看起来都正常

2024-04-29 01:59:27 发布

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

我有一个函数,它应该获取我在输入框中输入的日期,并使用它计算该范围内的一些数据,例如:从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()

Tags: andthe数据函数timeconnectcursordd
2条回答

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

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

感谢您的所有建议,特别是那些建议调试的建议。我的调试确实是一种巨大的压力,但最终我发现了我的错误

该错误与任何类型的拼写错误或数据类型无关。问题是错误地定位了d___输入变量和d_f_输入变量。在这里,我将再次发布代码的正确形式。再次感谢你们所有人,感谢你们投入时间帮助我。也很抱歉打扰大家

enter code here

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

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

相关问题 更多 >