Viktor.w
Viktor.w

Reputation: 2297

Transform a database query to a useful format

I have a table on my database, I fetched the first row of my table and the result looks like this:

(datetime.datetime(2018, 10, 3, 16, 27, 29, 198000), 'buy', Decimal('0.00276851'), Decimal('6.00000000'), 1, None)

However, on my database (datetime.datetime(2018, 10, 3, 16, 27, 29, 198000) is a timestamp but here it is a string. How can I transform this result into a timestamp format that I could use later on my code? Thanks!

This is my code so far:

conn = psycopg2.connect("dbname=monty user=postgres host=localhost password=*****")
cur = conn.cursor()
cur.execute("SELECT * FROM binance.zrxeth_aggregated FETCH first 1 rows only;")
row = cur.fetchone()
cur.close()
conn.close()

Upvotes: 1

Views: 55

Answers (1)

alecxe
alecxe

Reputation: 474003

According to the Python-to-Postgres types table psycopg2's documentation, this is the intended behavior. Postgres's timestamp becomes datetime object in Python.

Then, if you really need the timestamp value, it is just a matter of calling datetime.utcfromtimestamp() on your datetime object:

Upvotes: 1

Related Questions