When I run this following select query on the command line in Mysql:
SELECT date_sent FROM books;
I get this sort of result (eg) | 2024-10-06 |
but if I run the same select query from inside python:
# Select query
cur.execute("SELECT date_sent FROM books")
output = cur.fetchall()
for i in output:
print(i)
I get this as a result (eg) (datetime.date(2024, 10, 6),)
I don’t understand what object this has returned, and how I can use it as a date. I have tried splitting it like a text field, printing it as a list item, a tuple item or a dictionary item, but it seems untranslatable.
2
Answers
You are being returned a
datetime.date
instance, which can be formatted as a string using methodstrftime
.When you execute the query inside Python using
cur.fetchall()
it returnsdatetime.date
object. If you want it to be formatted as string you can usestrftime
. In below example I have formatted as ‘%Y-%m-%d’Output will be ‘2024-10-06’