Click here to Skip to main content
12,956,234 members (67,793 online)
Rate this:
 
Please Sign up or sign in to vote.
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 15-Nov-12 14:52pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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()
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,869
CHill60 3,550
Maciej Los 3,243
ppolymorphe 2,070
Jochen Arndt 1,973


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100