条件语句不按计划使用Python中的None类型

时间:2018-05-30 17:04:51

标签: python python-3.x

此脚本根据日期在表中插入“NULL”值。 但是还有另一个表包含'NULL'值,具体取决于它是否已被发送。在这种情况下,它还没有被发送,所以我想忽略它们。但代码仍在继续并给我这个追溯:

Traceback如下:

Traceback (most recent call last):
   File "C:\projects\docs\script.py", line 43, in <module>
     if dr3 < date_sql[0] < dt:
TypeError: '<' not supported between instances of 'datetime.datetime' and 'NoneType'

长期以来一直在苦苦挣扎,所以非常感谢您的指导,因为我试图找到解决方案。

Python代码是:

import pyodbc
from datetime import date, datetime
import dateutil.relativedelta

conn = pyodbc.connect(
r'DRIVER={SQL Server};'
r'SERVER=server;'
r'DATABASE=db;'
)

dt = datetime.today()
dr3 = dt - dateutil.relativedelta.relativedelta(months=3)

cursor = conn.cursor()

sent_date = cursor.execute("""SELECT sent_date, id
              FROM Department.Documents""")


def fetch_date():
   for row in sent_date:
       r = row
   return r


date_sql = fetch_date()


if sent_date != None:
   if dr3 < date_sql[0] < dt:
      try:
         value = None
         cursor.execute("""UPDATE Department.Customer SET name=?, address=?, email=?, 
                      phone=?""", (value, value, value, value))

         cursor.commit()

      except pyodbc.Error as ex:
         print(str(ex))

         cursor.rollback()

else:
  cursor.close()
  print("Connection closed")

0 个答案:

没有答案