I am trying to retrieve a row off of a table called players
, which contains a column named lastheal
, of type datetime
.
My code is as follows :
import mysql.connector
import datetime
class Player():
def __init__(self, name):
conn = mysql.connector.connect(host="xxxx", database="xxxx", user="xxxx", passwd="xxxx")
c = conn.cursor(prepared=True)
c.execute("""SELECT * FROM players WHERE nameplayer = %s""", (name,))
for player in c:
print player
I have no trouble retrieving rows from other tables that do not contain datetime
values. However, the above code produces following error :
File "~/player.py", line 14, in __init__
for player in c:
File "~/.local/lib/python2.7/site-packages/mysql/connector/cursor.py", line 1245, in fetchone
return self._row_to_python(row, self.description)
File "~/.local/lib/python2.7/site-packages/mysql/connector/cursor.py", line 1231, in _row_to_python
row = self._connection.converter.row_to_python(rowdata, desc)
File "~/.local/lib/python2.7/site-packages/mysql/connector/conversion.py", line 407, in row_to_python
result[i] = self._cache_field_types[field_type](row[i], field)
File "~/.local/lib/python2.7/site-packages/mysql/connector/conversion.py", line 506, in _DATETIME_to_python
(date_, time_) = value.split(b' ')
AttributeError: 'datetime.datetime' object has no attribute 'split'
How can I get mysql.connector
to retrieve rows with datetime
values in a correct way ?
You are getting this error because you are receiving the datetime
as an object. Hence why the error says 'datetime.datetime object
has no attribute 'split'. You are trying to run a string-based method on an object.
Try converting your data from the results into a string first using str()
. Then run your functions on it.