0

I have a MySQL database with ~300 values. The data is being inserted through a Java app, but I can't change anything there. The values that I am getting are:

|id|user|datetime|weight|

In that order. When I get the values from the database using python I get this (an example row):

(1, 'User', datetime.datetime(2019, 7, 19, 23, 48, 38), Decimal('159.8'))

I want to get the id, user and date(in yyyy-MM-dd format), and get the decimal, but I want the row to look like this:

(310, 'User', '2019-07-19', '159.8')

I have no idea what to do, or where to start. How can I get the values to change?

2 Answers 2

1

The result you get from the database is a tuple. Members of a tuple can't be changed.

To get a tuple in the form you want, it would be necessary to create a new tuple with the changed items.

Note that it would be necessary to include the datetime and decimal modules.

import datetime
from decimal import Decimal

new = []
i=(1, 'User', datetime.datetime(2019, 7, 19, 23, 48, 38), Decimal('159.8'))
new.append((i[0], i[1], i[2].strftime('%Y-%m-%d'), str(i[3])))

print(new)

This prints:

[(1, 'User', '2019-07-19', '159.8')]

I think this may solve the problem you have.

Sign up to request clarification or add additional context in comments.

Comments

1
date = datetime.datetime(2019, 7, 19, 23, 48, 38)
datetime.datetime.strftime(date, '%Y-%m-%d')

`'%Y-%m-%d'`` will get it into the yyyy-MM-dd format you desire.

You will need to import datetime first

2 Comments

Ok, but how would I extract the date from the row?
@RoXeno, if your tuple is t = (310, 'User', '2019-07-19', '159.8')' you can extract it with t[2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.