Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 code
def 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
Posted

1 solution

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()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900