Python - Convert mysql datatime

Asked

Viewed 89 times

1

I am trying to compare the current date with the date of the last entry of my mysql database:

sql2 = "SELECT created_at FROM tempaverage WHERE created_at IN (SELECT max(created_at) FROM tempaverage)"
  cursor.execute(sql2)
  current = strftime("%Y-%m-%d %H:%M:%S", gmtime())
  for (created_at) in cursor:
    diff = current-created_at
    print diff

But it doesn’t return anything when I do the print diff because it comes out in the exception and if you do the print created_at the value is > (datetime.datetime(2015, 3, 4, 11, 45, 3),)

How can I compare the dates?

1 answer

1

I decided to stop using the: strftime("%Y-%m-%d %H:%M:%S", gmtime())

then start using: datetime.datetime.now()

  current = datetime.datetime.now()
  for x in range(0,numrows):
    row = cursor.fetchone()
    created_at = row[0]
    diff = (current-created_at).seconds/60       

Browser other questions tagged

You are not signed in. Login or sign up in order to post.