0

I want to display a mysql table in Python, when I use the "Select * from table" command, the time and date column is displayed as a bunch of numbers with commas in between. Its hard to read them as date and time. My code is;

from datetime import datetime
import time
import mysql.connector as sq

s = sq.connect(host="localhost", user="root", passwd="Zxcvbnm*1", database="ip_project")
if s.is_connected():
    print("Sucess")

try:
    print("Displaying Attendance..")
    cur=s.cursor() 
    d="""select * from main"""
    cur.execute(d)
    data=cur.fetchall()
    for i in data:
        print(i)

except Exception:
    print("Something went wrong.. try again")

And the output is;

Sucess
Displaying Attendance..
(1, 'Talia', datetime.datetime(2021, 8, 16, 16, 28, 12))

I want to get rid of "datetime.datetime" and want it to look somewhat like

(1,'Talia',2021:8:16 16:28:12)

Is it possible? Also sorry for messy work. Just a beginner here, need this for school project.

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Pia
  • 3
  • 1
  • 1
    When you do `print(i)` you do not specify the way you want to see the output, so you get the Python default, which is to display the tuple as it would be represented as a literal in Python code. If you want to get rid of `datetime.datetime`, you need to extract the datetime from the tuple, as `i[2]`, then convert that to a string in the format you want, as `i[2].isoformat()`. If you want some other format, use `i[2].strftime(...)`. What you put in the parens depends on what format you want. You need to look at the documentation for that. So, `print (f"({i[0]}, {i[1]!r}, {i[2].isoformat()})")` – BoarGules Aug 17 '21 at 09:10
  • Thankyou so much! Got it working the way i wanted to with you help.. – Pia Aug 17 '21 at 09:39

1 Answers1

0

Read about strftime(format) method, to create a string representing the time under the control of an explicit format string.

Sergey Zaykov
  • 523
  • 2
  • 9