-1

CODE:

cur = sqlCon.cursor()

cur.execute("select datedue from library where member=%s ", Member.get())

row = cur.fetchone()
print(datetime.date.today())

for x in row:
    print(row)

But the result is in (datetime.date(2022, 12, 6),) fromat

What should I do?????

  • Nothing. You got back a proper Python `datetime` object. There's no format involved. Dates in databases are binary values. They have no format. It's the same with the `datetime` object. This is no different than trying to print eg a number. If you want to *print* that `datetime` in a certain way, you need to format it. That's done using `format`, print with a format string or an f-string – Panagiotis Kanavos Nov 22 '22 at 15:01
  • @PanagiotisKanavos can you explain how to use a format string – Nischoy Ghose Nov 22 '22 at 15:02
  • 1
    Does this answer your question? [How to print a date in a regular format?](https://stackoverflow.com/questions/311627/how-to-print-a-date-in-a-regular-format) – Panagiotis Kanavos Nov 22 '22 at 15:02
  • @PanagiotisKanavos I am new to python and sql so i am confused – Nischoy Ghose Nov 22 '22 at 15:02

1 Answers1

0

You may use the strftime() function:

cur.execute("SELECT datedue FROM library WHERE member = %s", Member.get())
row = cur.fetchone()
date_str = row["datedue"].strftime("%Y-%m-%d")
print(date_str)

You could also handle this on the MySQL side by using the STR_TO_DATE() function:

sql = "SELECT STR_TO_DATE(datedue, '%Y-%m-%d') AS datedue FROM library WHERE member = %s"
cur.execute(sql, Member.get())
row = cur.fetchone()
date_str = row["datedue"]
print(date_str)

Here STR_TO_DATE() returns a string on the database side, so no conversion would be needed in Python.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360