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
输出如下所示:[('20171023',),('20171024',),('20171025',),('20171026',),('20171027',),('20171028',) ,('20171029',)]
我希望日期采用列表格式。而且我不喜欢我们如何返回元组和2个逗号而不是1个逗号。有谁能解释一下我背后的原因是什么以及如何解决这个问题?
注意:需要postgres DB来运行它。
答案 0 :(得分:1)
也许是因为你使用的是表“日”而不是列。
这可能有效:
SELECT to_char(generate_series( '2017-10-23'::timestamp,'2017-10-29'::timestamp, '1 day'::interval), 'YYYYMMDD') AS day_f