Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python Python2.3 Python2.4 , +
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
0 OriginalGriff 300
1 Sergey Alexandrovich Kryukov 176
2 PhilLenoir 164
3 Richard MacCutchan 160
4 Sharmanuj 146
0 Sergey Alexandrovich Kryukov 6,081
1 OriginalGriff 5,115
2 CPallini 2,473
3 Richard MacCutchan 1,597
4 Abhinav S 1,505


Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
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