Hello guys.. I was wondering if any of you tried using DATE_FORMAT in python? you see I''m trying to format one of my field using date_format but the problem is when I try to run it in python it gives an error since % symbol is reserved char for python here is my python codedef get_announcement(con):
try:
c = con.cursor(DictCursor)
query = """
SELECT
id,
page_id,
title,
contents,
bnr_img,
DATE_FORMAT(start_date, ''%M %D, %Y''),
DATE_FORMAT(end_date, ''%M %D, %Y'')
FROM announcement
ORDER BY id DESC;
"""
c.execute(query)
dbres = c.fetchall()
if not dbres: return None
return dbres
finally:
if c: c.close()
But the problem is i cant get this to work since python is complaining about the formatting of the start_date and end_date do you have any idead on how to solve this in a query way. As much as possible I dont want to let python do the date formatting for me instead I want to retrieve the data that has been formatted already in mysql
Please give me your idead and answers thanks and more power
解决方案Never mind guys I already solved it... Just for the record here''s what I came up with
def get_announcement(con):
try:
c = con.cursor(DictCursor)
query = """
SELECT
id,
page_id,
title,
contents,
bnr_img,
DATE_FORMAT(start_date, %s),
DATE_FORMAT(end_date, %s)
FROM announcement
ORDER BY id DESC;
"""
format= "''%M %D, %Y''"
c.execute(query,[format,format])
dbres = c.fetchall()
if not dbres: return None
return dbres
finally:
if c: c.close()