classification
Title: String being confused with datetime.datetime object.
Type: compile error Stage:
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brandon, lemburg
Priority: normal Keywords:

Created on 2014-05-24 13:55 by brandon, last changed 2014-05-24 14:11 by brandon. This issue is now closed.

Messages (3)
msg219041 - (view) Author: Brandon (brandon) Date: 2014-05-24 13:55
Observe the following code:

import MySQLdb, MySQLdb.cursors, datetime
""" ... mysqlCursor is a cursor object from a connection to database from the MySQLdb module ... """
mysqlCursor.execute("SELECT NOW()")
timeRow = mysqlCursor.fetchall()
currentDateTime = datetime.datetime.strptime(timeRow[0]["NOW()"], "%Y-%m-%d %H:%M:%S")

I get the following error:

TypeError: must be string, not datetime.datetime

HOWEVER, when I cast timeRow[0]["NOW()"] to a string like: str(timeRow[0]["NOW()"]) , it works fine.

For whatever reason the Python interpreter seems to interpret the string from the row of the MySQLdb cursor result as a datetime.datetime object. I have no explanation for this, besides it looking like a date time in the format of YYYY-mm-dd HH:MM:SS. 

I have not tried this in Python 3.x, but the bug is in the latest compile of version 2.7.6 from the FTP distribution site.
msg219042 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2014-05-24 14:05
On 24.05.2014 15:55, Brandon wrote:
> 
> Observe the following code:
> 
> import MySQLdb, MySQLdb.cursors, datetime
> """ ... mysqlCursor is a cursor object from a connection to database from the MySQLdb module ... """
> mysqlCursor.execute("SELECT NOW()")
> timeRow = mysqlCursor.fetchall()
> currentDateTime = datetime.datetime.strptime(timeRow[0]["NOW()"], "%Y-%m-%d %H:%M:%S")
> 
> I get the following error:
> 
> TypeError: must be string, not datetime.datetime
> 
> HOWEVER, when I cast timeRow[0]["NOW()"] to a string like: str(timeRow[0]["NOW()"]) , it works fine.
> 
> For whatever reason the Python interpreter seems to interpret the string from the row of the MySQLdb cursor result as a datetime.datetime object. I have no explanation for this, besides it looking like a date time in the format of YYYY-mm-dd HH:MM:SS. 

It's likely that MySQLdb returns the datetime value as Python
datetime.datetime object, so not really surprising that you get
a TypeError.
msg219045 - (view) Author: Brandon (brandon) Date: 2014-05-24 14:11
Type returned as datetime, I was not familiar with the MySQLdb code. Sorry for the bad report.
History
Date User Action Args
2014-05-24 14:11:57brandonsetresolution: not a bug
2014-05-24 14:11:50brandonsetstatus: open -> closed

messages: + msg219045
2014-05-24 14:05:33lemburgsetnosy: + lemburg
messages: + msg219042
2014-05-24 13:55:14brandoncreate