My first Daniweb question!! Am at the front end of learning Python (3.2.3)

I'm doing a SQL query into a MySQL database for two fields, track_ID and track_duration. I'm getting a return of both, but instead of the duration value being expressed as 00:02:50, as it is stored in the database, my Python call is returning it as datetime.timedelta(0,170). I realize that is 170 seconds and is correct, but would like to replicate the formatting in the db.

How do I get it to return it in the form it was stored, instead of the datetime.timedelta function?

MySQL call is: cur.execute("Select track_id, track_duration from tracks where track_id = 342") track_duration is stored in my db as a time data type.

Recommended Answers

All 5 Replies

datetime.timedelta(0,170)

 >>> str(datetime.timedelta(0,170))
 '0:02:50'

Thanks. That was quick!! But I'd like it to store that value in the list that I am creating out of the result

for instance:

cur.execute("Select track_id, track_duration from tracks where track_id = 342")
for r in cur:
    print(r)

and I get this: (342, datetime.timedelta(0, 170))

but what I want is (342, 0:02:50) stored as r

So do

tracks = []
for track_id, track_duration in cur:
    print(track_id, str(track_duration))
    tracks.append((track_id, str(track_duration))

Great. Thanks much

In case it is solved you should close the thread with Mark Question Solved In case you desire, you can give well writen answers up/downvotes by clicking arrows on top right corner. In case somebody gave you good solution you may decide to instead of clicking the arrow to write a comment in pop up window and click Vote & Comment to add the reputation of the poster with your positive rep power.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.