Skip to main content
 首页 » 编程设计

Python Datetime 显示 Nonetype

2025年05月04日17rubylouvre

我用pymysql连接mysql

from datetime import datetime 
from datetime import timedelta 
import pymysql 
conn = pymysql.connect()#I simplified the connecting details 
 
print("date:  {}".format(db.fetchone()[0])) 
print("the type of the date: {}".format(type(db.fetchone()[0]))) 
print("one row : {}".format(db.fetchone())) 
print("one row of the type: {}".format(type(db.fetchone()))) 
print("the recent time: {}".format(datetime.now())) 
print("the recent time of the type: {}".format(type(datetime.now()))) 

date: 2017-06-01 10:00:00

the type of the date: one row :

(datetime.datetime(2017, 5, 31, 17, 0), None, 31.5, 88.7, None)

one row of the type:

the recent time: 2017-06-01 18:31:04.097299

the recent time of the type: 

但是

while ((time - conn.cursor().fetchone()[0] != timedelta(minutes=15))): 
    pass 
while ((time - datetime.now() != timedelta(minutes=15))): 
    pass 

前者出现了:

TypeError: 'NoneType' object is not subscriptable

为什么它们不是同一类型???

更新

我的 table :

create table weather (  
                time timestamp default current_timestamp on update current_timestamp, 
                id smallint, 
                tpr float(3,1), 
                wet float(3,1), 
                uv tinyint(2), 
                foreign key (id) references chatbot.station(pk)); 

请您参考如下方法:

似乎 db.fetchone() 返回了 None 而不是 datetime 对象。
您的循环是否可能到达了表格的末尾?