0
def db_connect():
   #Please assign variables in dict db = {"database":"", ...}
    try:
        conn = psycopg2.connect(database=db["database"], user=db["user"], password=db["password"], host=db["host"], port=db.get("port", "5432"))
    except:
        conn = None
        print "I am unable to connect to the database"
    return conn

def db_query_time():
    conn = db_connect()
    if conn is not None:
        cur = conn.cursor()
        query_generate_date_series = '''SELECT to_char(day, 'YYYYMMDD') as day_f FROM generate_series( '2017-10-23'::timestamp,'2017-10-29'::timestamp, '1 day'::interval) day ;'''
        cur.execute(query_generate_date_series)
        rows = cur.fetchall()
        print rows

Output looks like this: [('20171023',), ('20171024',), ('20171025',), ('20171026',), ('20171027',), ('20171028',), ('20171029',)]

I want dates to be in a list format. and I don't like how we are returning tuple and 2 commas instead of 1 comma. Can anyone please explain me what is the reason behind and how to fix this?

Note: Need postgres DB to run this.

2
  • Is this for a homework? Because this would be handled better in application code than through a DB. Commented Oct 31, 2017 at 23:05
  • No, Not home work. What is the library I can use in python? I tried datetime? Commented Nov 1, 2017 at 4:03

1 Answer 1

1

Perhaps it's because you're using a table "day" and not a column.

This might work:

SELECT to_char(generate_series( '2017-10-23'::timestamp,'2017-10-29'::timestamp, '1 day'::interval), 'YYYYMMDD') AS day_f
Sign up to request clarification or add additional context in comments.

Comments

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.