1

I use pymysql to connect 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: 

But

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

The former showed up:

TypeError: 'NoneType' object is not subscriptable

Why are't they the same type???

Update

MY 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));
Jonathan Cheng
  • 459
  • 2
  • 11
  • 26

3 Answers3

3

It seems like db.fetchone() returned None instead of a datetime object.
Is it possible your loop reached the end of your table?

Shai
  • 111,146
  • 38
  • 238
  • 371
2

Problem

When we use fetchone method and no records left to read it returns None which is not subscriptable.

Example

Assuming that we want to process non-null time values for all records using query

SELECT time FROM weather WHERE time IS NOT NULL

and time object is current datetime

time = datetime.now()

then we can write it using EAFP coding style like

# or any other `datetime` object
time = datetime.now()
with conn.cursor() as cursor:
    query = "SELECT time FROM weather WHERE time IS NOT NULL"
    cursor.execute(query)
    record = cursor.fetchone()
    try:
        recorded_time = record[0]
    except TypeError:
        # no records found in table for given query,
        # reporting
        print('No records found for query: "{query}"'
              .format(query=query))
    else:
        while time - recorded_time != timedelta(minutes=15):
            record = cursor.fetchone()
            try:
                recorded_time = record[0]
            except TypeError:
                # no more records found, leaving loop
                break

or with checking records for None'ness

# or any other `datetime` object
time = datetime.now()
with conn.cursor() as cursor:
    query = "SELECT time FROM weather WHERE time IS NOT NULL"
    cursor.execute(query)
    record = cursor.fetchone()
    if record is not None:
        recorded_time = record[0]
        while time - recorded_time != timedelta(minutes=15):
            record = cursor.fetchone()
            if record is None:
                # no record found, leaving loop
                break
            recorded_time = record[0]
    else:
        # no records found in table for given query,
        # reporting
        print('No records found for query: "{query}"'
              .format(query=query))

WARNING

Without adding time IS NOT NULL filtration we theoretically can get record with recorded_time equal to None, then difference

time - recorded_time

will raise TypeError since we cannot subtract None from datetime object.

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
  • @JonathanCheng: completed answer accordingly to your table definition, tell me if it can be improved – Azat Ibrakov Jun 01 '17 at 12:46
  • How can I identify the empty table and the none row?thx.Because I want to insert the value if the table is empty and stop insert the value if the time of the row is None. – Jonathan Cheng Jun 02 '17 at 17:49
1

Check your returned value for db.fetchone(), it seems that is returning a None, you can print it above for example

print(db.fetchone()[0])

make a print inside the while loop (is just a quick way, not always the best way is doing this), some of your values must be None, if only one is, the code breaks.

Solution

You can ask before do the minus math operation if your value is none or not, If it's not, do the maths, if it is do nothing

developer_hatch
  • 15,898
  • 3
  • 42
  • 75