简体   繁体   English

[TypeError:无法将datetime.datetime与元组进行比较]:如何使用Python和SQLite3将元组值与datetime值进行比较?

[英][TypeError: can't compare datetime.datetime to tuple]: How to compare a tuple value to a datetime value using Python & SQLite3?

I am using the SQLite3 module within Python and I currently have a table that stores a "date" as datetime. 我正在Python中使用SQLite3模块,并且当前有一个表将“日期”存储为日期时间。 However, whenever I try to retrieve that date from the table and compare it to today's date (eg today = datetime.datetime.today()), I get an error that says: 但是,每当我尝试从表中检索该日期并将其与今天的日期进行比较(例如,今天= datetime.datetime.today())时,都会收到一条错误消息:

if row > today:

TypeError: can't compare datetime.datetime to tuple" TypeError:无法将datetime.datetime与元组进行比较”

Here is a snippet of the code that I am having trouble with: 这是我遇到问题的代码片段:

for row in con.execute('select time_date as "d [datetime]" from fault_record'):

if row < today:
    print row

I am stumped on how to convert the tuple date to a datetime? 我很困惑如何将元组日期转换为日期时间?

Thanks! 谢谢!

Rows are returned as tuples of columns, even if there's only one column. 即使只有一列,也将以列的元组返回行。

Changing 改变中

for row in con.execute('select time_date as "d [datetime]" from fault_record'):

to

for date, in con.execute('select time_date as "d [datetime]" from fault_record'):

will iterate over the dates, instead of the row tuples. 将遍历日期,而不是行元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM