0

main.py

import json, urllib.request, requests
import psycopg2
from psycopg2.extras import execute_values


# Retrieve Json Data from Within API

url = "https://datahead.herokuapp.com/api/employeers/"
response = urllib.request.urlopen(url)
data = json.loads(response.read())


# ***** connect to the db *******
try:
    conn = psycopg2.connect("dbname='datahead' user='postgres' host='localhost' password='datahead'")
except:
    print("I am unable to connect to the database")

# cursor
cur = conn.cursor()

fields = [
    'id', #integer
    'name', #varchar
    'log_date', #date
    'log_time', #timestamp
    'login', #timestamp
    'logout' #timestamp
]

for item in data:
    my_data = [item[field] for field in fields]
    insert_query = "INSERT INTO employee VALUES (%s, %s, %s, %s, %s, %s)"
    cur.execute(insert_query, tuple(my_data))
    conn.commit()

# close the cursor
cur.close()

# close the connection
conn.close()

please visit my api data format https://datahead.herokuapp.com/api/employeers/

Error Through

Will it change here timestamp type, if yes so which data type i've to use? Line 56 what happened?

PGadmin type

1 Answer 1

1

I think login_time should be of type time without time zone in PostgresSQL.

But you would probably be better off using timestamp with time zone to represent date and time together.

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

4 Comments

in my login, logout and log_time i used "timestamp without time zone" and in this case only i want to represent my time that's why without timezone used and doesn't work.
i.sstatic.net/KUC7z.png please visit this image and here if i use with timezone then i can't calculation may be.
There is too little English in your last comment for me to understand. Also, text is better than images.
Yeah i've solved that. have to use there time instead of timestamp. Thanks @Laurenz Albe

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.